且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Json.NET JSONPath查询未返回预期结果

更新时间:2023-01-17 09:19:43

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).