Newtonsoft JSON.Net SelectToken 问题
Newtonsoft JSON.Net SelectToken Issue
我有以下查询和示例 JSON。
我在“http://jsonpath.com/”上试了一下,它按预期工作。
如果我在 VisualStudio 中尝试它 returns 没有结果。
$.Items.Services[?(@.Name == 'Another Service')].Url
这是JSON:
{
"Items": {
"Resource": {
"Id": "12345"
},
"Services": {
"service1": {
"Name": "My First Service",
"Type": "WS",
"Url": "https://server1/service1"
},
"service2": {
"Name": "Another Service",
"Type": "WS",
"Url": "https://server2/service2"
}
}
}
}
以及示例代码:
JObject obj = JObject.Parse(File.ReadAllText(@"d:\temp\sample.json"));
var matches = obj.SelectTokens("$.Items.Services[?(@.Name == 'Another Service')].Url");
if(matches != null)
{
foreach(var item in matches)
{
item.Replace(replacement); // this never gets executed
}
}
试试这个:
var matches = obj.SelectTokens("$.Items.Services[?(@..Name == 'Another Service')]..Url");
我有以下查询和示例 JSON。 我在“http://jsonpath.com/”上试了一下,它按预期工作。 如果我在 VisualStudio 中尝试它 returns 没有结果。
$.Items.Services[?(@.Name == 'Another Service')].Url
这是JSON:
{
"Items": {
"Resource": {
"Id": "12345"
},
"Services": {
"service1": {
"Name": "My First Service",
"Type": "WS",
"Url": "https://server1/service1"
},
"service2": {
"Name": "Another Service",
"Type": "WS",
"Url": "https://server2/service2"
}
}
}
}
以及示例代码:
JObject obj = JObject.Parse(File.ReadAllText(@"d:\temp\sample.json"));
var matches = obj.SelectTokens("$.Items.Services[?(@.Name == 'Another Service')].Url");
if(matches != null)
{
foreach(var item in matches)
{
item.Replace(replacement); // this never gets executed
}
}
试试这个:
var matches = obj.SelectTokens("$.Items.Services[?(@..Name == 'Another Service')]..Url");