Newtonsoft JSON.Net SelectToken Issue

2020-01-29 18:27发布

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条回答
Root(大扎)
2楼-- · 2020-01-29 18:58

Try this:

var matches = obj.SelectTokens("$.Items.Services[?(@..Name == 'Another Service')]..Url");
查看更多
登录 后发表回答