首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用jsonpath表达式的数组大小- Stefan Goessner JsonPath

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

Stack Overflow用户
提问于 2016-05-01 09:06:28
回答 5查看 64K关注 0票数 32

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

我的jsonpath表达式是$.orders.length,JSON看起来如下所示:

代码语言:javascript
复制
{
  "orders" : [
    ...
  ]
}

它失败了,有以下错误:

代码语言:javascript
复制
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表达式获取数组的长度。

下面是如何获得配置的编辑

代码语言:javascript
复制
    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);
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2016-05-01 14:21:03

似乎对返回数组的length()的支持只是jayway json路径库的在2.1.0版中添加

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

票数 76
EN

Stack Overflow用户

发布于 2022-08-22 08:46:40

计算json数组中元素数的方法--这是我解决它的方法

myJson ->My Json,在其内部存在一个数组。

fieldPath ->数组的路径

代码语言:javascript
复制
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();
                       }
票数 0
EN

Stack Overflow用户

发布于 2022-11-07 10:25:45

使用webclient实现春季网络流量

代码语言:javascript
复制
....
 .jsonPath("$.length()").isEqualTo(2);
...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36964936

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档