简体   繁体   中英

Accessing multiple arrays in JSON using C#

My JSON looks like this:

    {"2013" : [
        { "date":"2013-01-09 12:00:00","height":0 },
        { "date":"2013-01-19 12:00:00","height":3 },
        { "date":"2013-01-29 12:00:00","height":2 }],
    "2012" : [
        { "date":"2012-02-09 12:00:00","height":0 },
        { "date":"2012-02-19 12:00:00","height":4 },
        { "date":"2012-02-29 12:00:00","height":2 }],
    "2011" : [
        { "date":"2011-03-09 12:00:00","height":3 },
        { "date":"2011-03-19 12:00:00","height":8 },
        { "date":"2011-03-29 12:00:00","height":2 }]
   }

What I am trying to do is get all of the dates and height, but I am only able to do it per year using this code:

    public class Report
    {
        public DateTime Date { get; set; }
        public int Height { get; set; }
    }

    JObject data = JObject.Parse(sr);
    var postTitles = from p in data["2013"]
                     select new Report
                     {
                         Date = DateTime.Parse((string)p["date"]),
                         Height = (int)p["height"]
                     };

I was thinking of using a for loop but trying a variable inside data[" "] won't work. I was also thinking of doing var postTitles += statement but it is not allowed as well. Any ideas on how I should go about this? I am using JSON.NET and it is suggested that I do a class per year (ie 2013, 2012, 2011) but I want them under one class to make it easier to manipulate data.

I am not that great with the LINQ syntax you are using but what you want to do is create a list from many lists. So you want to use the SelectMany mapping operation from LINQ.

var postTitles = data.Children()
        .SelectMany(subitems => subitems.First)
        .Select(dataOfYear =>
            new Report
                {
                    Date = DateTime.Parse((string)dataOfYear ["date"]),
                    Height = (int)dataOfYear ["height"]
                }
            );

If you want only the first 100 reports of every year you could do it like this:

var postTitles = data.Children()
        .SelectMany(subitems => subitems.First)
        .Take(100)
        .Select(dataOfYear =>
            new Report
                {
                    Date = DateTime.Parse((string)dataOfYear ["date"]),
                    Height = (int)dataOfYear ["height"]
                }
            );

Or if you just want to free up the UI thread you could run this in a background thread:

var postTitles = await Task.Run(() => data.Children()
        .SelectMany(subitems => subitems.First)
        .Select(dataOfYear =>
            new Report
                {
                    Date = DateTime.Parse((string)dataOfYear ["date"]),
                    Height = (int)dataOfYear ["height"]
                }
            ));

I turned and twisted Mark's anwer somewhat and came up with this:

var postTitles = data.Children() // Select array container properties, like "2013"
        .SelectMany(x => x.First) // Select each subitem from every array
        .Select(r => // Create a report for each item
            new Report
                {
                    Date = DateTime.Parse((string)r["date"]),
                    Height = (int)r["height"]
                }
            );

Hopefully the inline comments will explain the logic sufficiently.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM