【发布时间】:2019-01-20 10:54:34
【问题描述】:
我正在寻找使用 SelectTokens(JPath) 获取部分 JSON 字符串而不是 JTOken 集合的最佳方法。
例如:
JObject o = JObject.Parse(@"{
'Stores': [
'Lambton Quay',
'Willis Street'
],
'Manufacturers': [
{
'Name': 'Acme Co',
'Products': [
{
'Name': 'Anvil',
'Price': 50
}
]
},
{
'Name': 'Contoso',
'Products': [
{
'Name': 'Elbow Grease',
'Price': 99.95
},
{
'Name': 'Headlight Fluid',
'Price': 4
}
]
}
]
}");
List<JToken> manufactures = o.SelectTokens("Manufacturers");
我需要输出 JSON 字符串而不是 JToken 集合。
预期输出:
{
"Manufacturers": [
{
"Name": "Acme Co",
"Products": [
{
"Name": "Anvil",
"Price": 50
}
]
},
{
"Name": "Contoso",
"Products": [
{
"Name": "Elbow Grease",
"Price": 99.95
},
{
"Name": "Headlight Fluid",
"Price": 4
}
]
}
]
}
有没有办法得到这样的输出?
【问题讨论】:
-
您是否正在从this answer 到How to perform partial object serialization providing “paths” using Newtonsoft JSON.NET 寻找类似
JsonExtensions.RemoveAllExcept(this JObject obj, IEnumerable<string> paths)的内容?