本文介绍了Newtonsoft JSON.Net SelectToken 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:	empsample.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 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 20:54
查看更多