Newtonsoft JSON.Net SelectToken问题

时间:2016-09-12 01:57:08

标签: json.net jsonpath

我有以下查询和示例JSON。 我试试" http://jsonpath.com/"它按预期工作。 如果我在VisualStudio中尝试它,则不会返回任何结果。

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

1 个答案:

答案 0 :(得分:1)

试试这个:

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