Accessing multiple arrays in JSON using C#

青春壹個敷衍的年華 提交于 2019-12-11 08:14:14

问题


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 (i.e. 2013, 2012, 2011) but I want them under one class to make it easier to manipulate data.


回答1:


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.




回答2:


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"]
                }
            ));


来源:https://stackoverflow.com/questions/17587344/accessing-multiple-arrays-in-json-using-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!