Linq to json 使用 Where 子句过滤结果
Linq to json filtering results with Where clause
我是 json 的新手。我正在尝试在 C# 中使用 linq to json 查询来过滤 json 数据。
我正在尝试从 json 数据中检索多个值:
string json= @"{
"parts": [
{
"attributes": {
"Motherboard": "Gigabyte GA-H81M-S2H",
"Max RAM": "16GB",
"Form Factor": "Micro ATX",
"RAM Slots": 2,
"Socket / CPU": "LGA1150"
},
"type": "Motherboard",
"name": "Gigabyte GA-H81M-S2H",
"slug": "gigabyte-motherboard-gah81ms2h"
},
{
"attributes": {
"Motherboard": "MSI H55-G43",
"Max RAM": "16GB",
"Form Factor": "ATX",
"RAM Slots": 4,
"Socket / CPU": "LGA1156"
},
"type": "Motherboard",
"name": "MSI H55-G43",
"slug": "msi-motherboard-h55g43"
},
{
"url": "http://pcpartpicker.com/part/asus-motherboard-rampageivblackedition",
"attributes": {
"Motherboard": "Asus Rampage IV Black Edition",
"Max RAM": "128GB",
"Form Factor": "EATX",
"RAM Slots": 8,
"Socket / CPU": "LGA2011"
},
"type": "Motherboard",
"name": "Asus Rampage IV Black Edition",
"slug": "asus-motherboard-rampageivblackedition"
}
],
}";
这是我的 C# 代码:
JObject results = JObject.Parse(json);
var categories = from c in results["parts"]["attributes"].Children()["Motherboard"].Values<string>()
group c by c
into g
orderby g.Count() descending
select new { Category = g.Key, Count = g.Count() };
我试过这段代码,但 returns 没有任何问题 result.Please 让我知道我哪里做错了,或者这是编写 query.Can 的正确方法吗?有人请帮忙我来解决这个问题。
您可以使用 SelectTokens
for queries of this nature. It supports JSONPath 查询语法。因此:
var categories = from c in results.SelectTokens("parts[*].attributes.Motherboard")
group c by c
into g
orderby g.Count() descending
select new { Category = (string)g.Key, Count = g.Count() };
在你的JSON中,"parts" : [...]
的值是一个数组;在查询中,"parts[*]"
是一个通配符,用于搜索数组的所有元素。
我是 json 的新手。我正在尝试在 C# 中使用 linq to json 查询来过滤 json 数据。 我正在尝试从 json 数据中检索多个值:
string json= @"{
"parts": [
{
"attributes": {
"Motherboard": "Gigabyte GA-H81M-S2H",
"Max RAM": "16GB",
"Form Factor": "Micro ATX",
"RAM Slots": 2,
"Socket / CPU": "LGA1150"
},
"type": "Motherboard",
"name": "Gigabyte GA-H81M-S2H",
"slug": "gigabyte-motherboard-gah81ms2h"
},
{
"attributes": {
"Motherboard": "MSI H55-G43",
"Max RAM": "16GB",
"Form Factor": "ATX",
"RAM Slots": 4,
"Socket / CPU": "LGA1156"
},
"type": "Motherboard",
"name": "MSI H55-G43",
"slug": "msi-motherboard-h55g43"
},
{
"url": "http://pcpartpicker.com/part/asus-motherboard-rampageivblackedition",
"attributes": {
"Motherboard": "Asus Rampage IV Black Edition",
"Max RAM": "128GB",
"Form Factor": "EATX",
"RAM Slots": 8,
"Socket / CPU": "LGA2011"
},
"type": "Motherboard",
"name": "Asus Rampage IV Black Edition",
"slug": "asus-motherboard-rampageivblackedition"
}
],
}";
这是我的 C# 代码:
JObject results = JObject.Parse(json);
var categories = from c in results["parts"]["attributes"].Children()["Motherboard"].Values<string>()
group c by c
into g
orderby g.Count() descending
select new { Category = g.Key, Count = g.Count() };
我试过这段代码,但 returns 没有任何问题 result.Please 让我知道我哪里做错了,或者这是编写 query.Can 的正确方法吗?有人请帮忙我来解决这个问题。
您可以使用 SelectTokens
for queries of this nature. It supports JSONPath 查询语法。因此:
var categories = from c in results.SelectTokens("parts[*].attributes.Motherboard")
group c by c
into g
orderby g.Count() descending
select new { Category = (string)g.Key, Count = g.Count() };
在你的JSON中,"parts" : [...]
的值是一个数组;在查询中,"parts[*]"
是一个通配符,用于搜索数组的所有元素。