Json.NET JSONPath查询未返回预期结果 [英] Json.NET JSONPath query not returning expected results

查看:83
本文介绍了Json.NET JSONPath查询未返回预期结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Newtonsoft的Json.Net从以下json中选择节点:

I'm using Newtonsoft's Json.Net to select nodes from the following json:

{  
   "projects":[  
      {  
         "name":"Project 1",
         "client":{  
            "code":"ABC",
            "name":"Client 1"
         }
      },
      {  
         "name":"Project 2",
         "client":{  
            "code":"DEF",
            "name":"Client 2"
         }
      },
      {  
         "name":"Project 3",
         "client":{  
            "code":"GHI",
            "name":"Client 3"
         }
      }
   ]
}

以下c#代码段

//json is a JObject representation of the json listed above
var clients =  json.SelectTokens("$.projects[*].client");

收益:

[  
   {  
      "code":"ABC",
      "name":"Client 1"
   },
   {  
      "code":"DEF",
      "name":"Client 2"
   },
   {  
      "code":"GHI",
      "name":"Client 3"
   }
]

现在很酷,我想做的是按客户端代码过滤,我认为

Which is cool, now, what I'd like to do is filter by client code, I would think that

$.projects[*].client[?(@.code == 'DEF')]

可以工作,但是我显然对语法不太了解.这将返回一个空列表:

would work, but I obviously don't understand the syntax well enough. This returns an empty list:

 var test1 =  json.SelectTokens("$.projects[*].client[?(@.code == 'DEF')]").ToList();

单个令牌选择器返回空值:

And the single token selector returns a null:

  var test2 =  json.SelectToken("$.projects[*].client[?(@.code == 'DEF')]");

我在 https://jsonpath.curiousconcept.com/上尝试了几种不同的配置,看来我查询语法确实坏了.

I tried a few different configurations on https://jsonpath.curiousconcept.com/ and it seems my query syntax is indeed broken.

使用Flow Communications的JSONPath 0.3.4实现(在上面的链接上),我可以使用

Using Flow Communications' JSONPath 0.3.4 implementation (on the above link) I can get the client using

$..[?(@.code == 'DEF')]

但是,此语法似乎对Json.Net(在同一页面上的Goessner实现也无效)无效.

however, this syntax does not seem valid for Json.Net (nor the Goessner implementation on the same page).

有人看到我在做什么错吗?

Anyone see what I'm doing wrong?

推荐答案

Json.NET的

Json.NET's JSONPath parser allows the filter (script) expression [?( )] to query for nested properties inside child objects of the candidate array item(s). Thus

var test = json.SelectTokens(@"$.projects[?(@.client.code == 'DEF')].client");

根据需要返回

[
  {
    "code": "DEF",
    "name": "Client 2"
  }
]

正在使用 .Net小提琴.

在实验中,似乎 jsonpath.curiousconcept.com 上的JSONPath实现都不支持这种语法,但是它在使用 jsonpath.com 上正常工作-online-evaluator"rel =" nofollow noreferrer> https://github.com/ashphy/jsonpath-online-evaluator . JSONPath建议使用底层脚本简单地指出()脚本表达式引擎,这显然为该应该"如何以及如何工作提供了解释空间.

Upon experimentation it seems none of the JSONPath implementations at jsonpath.curiousconcept.com support this syntax, however it works correctly at jsonpath.com which uses https://github.com/ashphy/jsonpath-online-evaluator. The JSONPath proposal simply states that () is a script expression, using the underlying script engine which clearly leaves some room for interpretation as to whether and how this "should" work.

问题中使用的过滤器显然不适用于Json.NET,因为从10.0.3版开始,它仅正式支持将过滤器应用于数组项,而不是直接应用于对象-请参见问题#1256 :JSONPath脚本无法正确执行对象以进行讨论. (尽管有时可以找到一些狡猾的解决方法,例如在此答案中.)但是,在过滤器中使用嵌套属性似乎可以打算工作(并且确实可以工作).

The filters used in the question apparently do not work with Json.NET because, as of version 10.0.3, it only officially supports applying filters to array items and not directly to objects -- see Issue #1256: JSONPath scripts not executing correctly for objects for a discussion. (Though sometimes cunning workarounds can be found, for instance in this answer.) Using nested properties inside filters, however, seems to be intended to work (and does work).

这篇关于Json.NET JSONPath查询未返回预期结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆