使用C#访问JSON中的多个数组
本文关键字:数组 访问 JSON 使用 | 更新日期: 2023-09-27 18:21:59
我的JSON如下所示:
{"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 }]
}
我想做的是获取所有的日期和身高,但我每年只能使用以下代码:
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"]
};
我曾想过使用for循环,但在data["]中尝试变量是行不通的。我也在考虑做varpostTitles+=语句,但这也是不允许的。我该怎么做有什么想法吗?我使用的是JSON.NET,建议我每年(即2013、2012、2011)做一个类,但我希望它们在一个类下,这样更容易操作数据。
我对您使用的LINQ语法不太熟悉,但您想做的是从许多列表中创建一个列表。因此,您希望使用LINQ中的SelectMany映射操作。
var postTitles = data.Children()
.SelectMany(subitems => subitems.First)
.Select(dataOfYear =>
new Report
{
Date = DateTime.Parse((string)dataOfYear ["date"]),
Height = (int)dataOfYear ["height"]
}
);
如果你只想要每年的前100份报告,你可以这样做:
var postTitles = data.Children()
.SelectMany(subitems => subitems.First)
.Take(100)
.Select(dataOfYear =>
new Report
{
Date = DateTime.Parse((string)dataOfYear ["date"]),
Height = (int)dataOfYear ["height"]
}
);
或者,如果你只是想释放UI线程,你可以在后台线程中运行:
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"]
}
));
我转过身来,稍微扭了一下马克的手,想出了这个:
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"]
}
);
希望内联注释能够充分解释逻辑。