我在使用Stefan Goessner的JsonPath查找数组或列表大小时遇到了问题。我使用的是json 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);发布于 2016-05-01 14:21:03
似乎对返回数组的length()的支持只是jayway json路径库的在2.1.0版中添加。
基于一些快速测试,$.orders.length()表达式似乎同时适用于2.1.0和2.2.0版本,因此我认为您只需要升级依赖版本,就可以修复所看到的错误。
发布于 2022-08-22 08:46:40
计算json数组中元素数的方法--这是我解决它的方法
myJson ->My Json,在其内部存在一个数组。
fieldPath ->数组的路径
import com.jayway.jsonpath.JsonPath;
import org.json.JSONArray;
import org.json.simple.JSONObject;
JSONObject myJson;
public long CountFields(String fieldPath) {
String onlyMyArray=JsonPath.parse(myJson).read(fieldPath).toString();
JSONArray jsonArray = new JSONArray(onlyMyArray);
return jsonArray.length();
}发布于 2022-11-07 10:25:45
使用webclient实现春季网络流量
....
.jsonPath("$.length()").isEqualTo(2);
...https://stackoverflow.com/questions/36964936
复制相似问题