使用 jsonpath 表达式的数组大小 - Stefan Goessner JsonPath

新手上路,请多包涵

我在使用 Stefan Goessner 的 JsonPath 查找数组或列表大小时遇到问题。我使用的是版本 json-path-2.0.0。

我的 jsonpath 表达式是 $.orders.length JSON 看起来像这样:

 {
  "orders" : [
    ...
  ]
}

它因以下错误而失败:

 com.jayway.jsonpath.PathNotFoundException: Property ['length'] not found in path $['orders']

我也尝试过 $.orders.length() 再次失败并出现以下错误:

com.jayway.jsonpath.PathNotFoundException: Property ['length()'] not found in path $['orders']

请建议我如何使用 Goessner 的 JsonPath 表达式获取数组的长度。

[编辑] 以下是我获取配置的方式:

     com.jayway.jsonpath.Configuration conf = com.jayway.jsonpath.Configuration.defaultConfiguration().addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL);
    DocumentContext documentContext = JsonPath.using(conf).parse(orderJson);
    Object val = documentContext.read(jsonPathExpression);

原文由 Nagendra Varma 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 800
2 个回答

似乎只在 jayway json-path 库的 2.1.0 版中添加了 对返回数组的 length() 的支持。

基于一些快速测试, $.orders.length() 表达式似乎适用于版本 2.1.0 和版本 2.2.0,所以我认为你只需要升级你的依赖版本来修复你看到的错误.

原文由 andersschuller 发布,翻译遵循 CC BY-SA 3.0 许可协议

spring webflux 使用 webclient

 ....
 .jsonPath("$.length()").isEqualTo(2);
...

原文由 bmck 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏