Newtonsoft JSON.Net SelectToken问题 [英] Newtonsoft JSON.Net SelectToken Issue
本文介绍了Newtonsoft JSON.Net SelectToken问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下查询和示例JSON. 我在" http://jsonpath.com/上尝试了,它按预期工作. 如果我在Visual Studio中尝试过,则不会返回任何结果.
I have the following query and the sample JSON. I try it on "http://jsonpath.com/" it works as expected. If I try it in VisualStudio it returns no results.
$.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");
这篇关于Newtonsoft JSON.Net SelectToken问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文