给出以下JSON:
{
"items": {
"primary": "structured",
"item0": {
"keyA1": "value1",
"keyA2": "value2",
"keyA3": "value3"
},
"item1": {
"keyB1": "value1",
"keyB2": "value2",
"keyB3": "value3"
},
"item2": {
"keyC1": "value1",
"keyC2": "value2",
"keyC3": "value3"
}
}
}
是否可以只获取那些键以" item"开头的对象,即" item0"," item1"和" item2&# 34 ;?最好使用Json.NET和/或LINQ,但JSONPath表达式也可以(我尝试在JSONPath表达式中使用星号来匹配item*
,但它不起作用)。
答案 0 :(得分:2)
当然,您可以使用Json.Net的LINQ-to-JSON API:
执行此操作var items = JObject.Parse(json)["items"]
.Children<JProperty>()
.Where(jp => jp.Name.StartsWith("item"))
.Select(jp => (JObject)jp.Value)
.ToList();
foreach (var item in items)
{
foreach (var kvp in item)
{
Console.WriteLine(kvp.Key + ": " + kvp.Value);
}
Console.WriteLine();
}