我在使用 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: 在路径 $['orders'] 中未找到属性 ['length()']
请建议我如何使用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);
java.lang.AssertionError: No value at JSON path "$.length"
。但是"$.length()"
可以正常工作。 - Võ Quang Hòa$.length()
在 json-path 2.7 上起作用。 - undefined