所以我正在尝试解析一些json数据..我想获取所有"id"
来自每个 "owner"
字符串列表中的节点,但我似乎无法通过这样做来访问节点。
string theData = File.ReadAllText("data.txt");
dynamic json = JObject.Parse(theData);
数据如下 https://hatebin.com/tqfvnp5ndgyn
如何正确获取所有 "id"
来自每个 "owner"
节点并将它们放入 new List<string>
?
最佳答案
查看您的特定 JSON 对象是否适合 LINQ 查询。
例如:
https://www.newtonsoft.com/json/help/html/QueryingLINQtoJSON.htm
var postTitles =
from p in rss["channel"]["item"]
select (string)p["title"];
...或...
var categories =
from c in rss["channel"]["item"].SelectMany(i => i["categories"]).Values<string>()
group c by c
into g
orderby g.Count() descending
select new { Category = g.Key, Count = g.Count() };
上述示例中使用的源 JSON:
string json = @"{
'channel': {
'title': 'James Newton-King',
'link': 'http://james.newtonking.com',
'description': 'James Newton-King\'s blog.',
'item': [
{
'title': 'Json.NET 1.3 + New license + Now on CodePlex',
'description': 'Announcing the release of Json.NET 1.3, the MIT license and the source on CodePlex',
'link': 'http://james.newtonking.com/projects/json-net.aspx',
'categories': [
'Json.NET',
'CodePlex'
]
},
{
'title': 'LINQ to JSON beta',
'description': 'Announcing LINQ to JSON',
'link': 'http://james.newtonking.com/projects/json-net.aspx',
'categories': [
'Json.NET',
'LINQ'
]
}
]
}
}";
关于c# - 如何获取具有特定名称的所有节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62253604/