数组的Java JsonPath长度

Posted

技术标签:

【中文标题】数组的Java JsonPath长度【英文标题】:Java JsonPath length of array 【发布时间】:2018-09-16 16:03:14 【问题描述】:

我有一个这样的 Json:


    "hello": 
        "hello": [
            
                "wwrjgn": "aerg",
                "aaa": "gggg",
                "rfshs": 
                    "segse": "segsegs",
                    "xx": "rgwgwgw",
                    "x-e ": "eergye"
                ,
                "egg": "sgsese",
                "segess": "sgeses",
                "segess": "segess"
            ,
            
                "esges": "segesse",
                "segesg": "ws",
                "rddgdr": 
                    "rdrdrdg": “srgsesees"
                ,
                "drr": 3600,
                "esese": "uytk",
                "wew": "699",
                "eses": “qe4ty"
            
        ],
        "how": www"
    

我执行以下操作:

Object document = 
Configuration.defaultConfiguration().addOptions().jsonProvider()
            .parse(ka.toJSONString());
int queries = JsonPath.read(document, "$..hello.length()");

    System.out.println("THE LENGTH OF THE NUMBER OF QUERIES " + queries);

我明白了:

nested exception is java.lang.ClassCastException: net.minidev.json.JSONArray 
cannot be cast to java.base/java.lang.Integer] with root cause
java.lang.ClassCastException: net.minidev.json.JSONArray cannot be cast to 
java.base/java.lang.Integer

在这里尝试:http://jsonpath.herokuapp.com/?path=$..hello.length() 我明白了:

[
   2,
   2
]

这是我需要的。

知道我做错了什么吗? 提前致谢。

【问题讨论】:

正如例外所说:您使用不兼容的类型。您不能将JSONArray(这似乎是JsonPath.read(...) 方法的结果)转换为Integer @Turing85 当我做 JSONArray = 而不是 int = 我得到: java.lang.ClassCastException: net.minidev.json.JSONArray cannot be cast to org.json.simple.JSONArray 尝试 List 或 List 作为返回值 嗯...如果您使用两个不同的JSONArray 类,这是预期的。 @Guts 现在我没有得到异常,但对于这两种情况我都得到:查询数的长度 [] 【参考方案1】:

这...

JsonPath.read(document, "$..hello.length()")

... 将其响应转换为 net.minidev.json.JSONArray 的实例,该实例不能转换为 int。这解释了ClassCastException

你看到的原因……

[
   2,
   2
]

...尝试在线评估器时,这是 toString()JSONArray 实例返回该字符串。

要在代码中获取“查询数的长度”,只需将 JsonPath 输出读取为 JSONArray,然后获取该类型的大小属性。例如:

JSONArray read = JsonPath.read(document, "$..hello.length()");
// prints:
//    THE LENGTH OF THE NUMBER OF QUERIES 2
System.out.println("THE LENGTH OF THE NUMBER OF QUERIES " + read.size());

您也可以返回List<Integer,例如:

List<Integer> read = JsonPath.read(document, "$..hello.length()");
// prints:
//    THE LENGTH OF THE NUMBER OF QUERIES 2
System.out.println("THE LENGTH OF THE NUMBER OF QUERIES " + read.size());

【讨论】:

您的回答在语义上是合理的,但似乎有一个错误:OP 似乎导入了不同的 JSONArray 类(org.json.simple.JSONArray)。在这种情况下,您可能需要使用 FQDN(有关详细信息,请参阅问题的评论部分)。 我现在得到以下异常:严重:Servlet.service() for servlet [dispatcherServlet] in context with path [] throw exception [Request processing failed;嵌套异常是 java.lang.ClassCastException: net.minidev.json.JSONArray cannot be cast to org.json.simple.JSONArray] 根本原因 java.lang.ClassCastException: net.minidev.json.JSONArray cannot be cast to org. json.simple.JSONArray JSONArray 处抛出异常 read = JsonPath.read(document, "$..hello.length()"); @Turing85 我之前使用过 jsonpath 来实际解析和读取并且这些工作.. 它只是数组长度不起作用.. 不确定这是否重要 @user6671584 我已经更新了答案以解决JSONArray 类型之间的混淆。正如@Turing85 所说,您可以使用 FQDN (net.minidev.json.JSONArray) 或只使用 List&lt;Integer&gt;,就像我在最新更新中展示的那样。

以上是关于数组的Java JsonPath长度的主要内容,如果未能解决你的问题,请参考以下文章

JSONPath 表达式的使用

JMeter获取json响应报文中数组长度

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

JSON长度

遍历 JsonPath 数组的策略

使用 jsonpath_ng 检查数组中的元素失败并出现 JsonPathParseError