Newtonsoft JSON.Net SelectToken Issue

孤街醉人 提交于 2019-12-17 14:55:25

问题


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

Here's the 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"
        }
    }
}   
}

And the sample code:

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
    }
 }

回答1:


Try this:

var matches = obj.SelectTokens("$.Items.Services[?(@..Name == 'Another Service')]..Url");


来源:https://stackoverflow.com/questions/39442443/newtonsoft-json-net-selecttoken-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!