我在使用 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 许可协议
似乎只在 jayway json-path 库的 2.1.0 版中添加了 对返回数组的
length()
的支持。基于一些快速测试,
$.orders.length()
表达式似乎适用于版本 2.1.0 和版本 2.2.0,所以我认为你只需要升级你的依赖版本来修复你看到的错误.