Ayoub_B
Ayoub_B

Reputation: 700

Getting the name of array

I have the following Json:

{
    "last_ts": "20161001154251",
    "first_ts": "20061130072151",
    "years": {
        "2006": [
            0,0,0,2,0,0,0,0,0,0,1,0
        ],
        "2007": [
            0,0,3,0,0,1,0,0,0,0,1,0
        ],
        "2008": [.........],
        .
        .
        .
    }
}

I wanted to read each year's name and its corresponding array of numbers, I tried the following code:

JObject jObj = JObject.Parse(json);

var yearsA = jObj["years"];
foreach (var year in yearsA)
{
    string yearStr = year. // code here to retrieve this year's name
    foreach (var month in year.Children<JArray>().Children()) // loop thru each array value
    {
        int m = (int)month;
        if(m > 0)
        {
            years.Add(yearStr);
            break;
        }
    }
} 

What I want now is just a way to get the name of the array, I tried lot of solutions but none worked for me.

Upvotes: 0

Views: 1177

Answers (3)

Givi
Givi

Reputation: 1734

If you want to use Linq to JSON, you can do something like this, but there's lot of other options, as mentioned in another answers.

 string json = @"{
            'last_ts': '20161001154251',
            'first_ts': '20061130072151',
            'years': {
                '2006': [
                    0,0,0,2,0,0,0,0,0,0,1,0
                ],
                '2007': [
                    0,0,3,0,0,1,0,0,0,0,1,0
                ],
            }
        }";

        JObject jObj = JObject.Parse(json);
        // since in your case your years and months are structure as key/value, it's possible to use built in class like Dictionary<TKey, TValue>
        var years = jObj["years"].ToObject<Dictionary<string, List<int>>>();

        var result = new Dictionary<string, List<int>>();

        foreach (var year in years)
        {
            string key = year.Key;
            var value = year.Value;
            var months = new List<int>();

            value.ForEach(t =>
            {
                if (t > 0)
                {
                    months.Add(t);
                }
            });

            result.Add(key, months);
        }

Look at Convert JObject into Dictionary. Is it possible?

and How do I get the list of keys in a dictionary?

Upvotes: 0

L.B
L.B

Reputation: 116108

Just declare a class like

public  class MyObj
{
    public string last_ts { set; get; }
    public string first_ts { set; get; }
    public Dictionary<int,int[]> years { set; get; }
}

and deserialize as

var data = JsonConvert.DeserializeObject<MyObj>(jsonString);

Sample usage:

foreach(var entry in data.years)
{
    int year = entry.Key;
    int[] months = entry.Value.Where(m => m > 0).ToArray();
    Console.WriteLine(year + " => " + string.Join(",", months));
}

Upvotes: 1

Aleks Andreev
Aleks Andreev

Reputation: 7054

Try this code:

var yearsA = jObj["years"].Cast<JProperty>();
List<string> years = new List<string>();
foreach (var year in yearsA)
{
    foreach (var month in year.Children<JArray>().Children()) // loop thru each array value
    {
        int m = (int) month;
        if (m > 0)
        {
            years.Add(year.Name);
            break;
        }

    }
}

Upvotes: 1

Related Questions