【发布时间】: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 现在我没有得到异常,但对于这两种情况我都得到:查询数的长度 []
标签: java arrays json spring-boot jsonpath