[英]Java JsonPath length of array
I have a Json like this: 我有一个像这样的杰森:
{
"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"
}
}
I do the following: 我执行以下操作:
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);
I get: 我得到:
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
Trying it here: http://jsonpath.herokuapp.com/?path= $..hello.length() I get: 在这里尝试: http ://jsonpath.herokuapp.com/?path= $ .. hello.length()我得到:
[
2,
2
]
Which is what i need. 这就是我所需要的。
Any idea what i am doing wrong? 知道我在做什么错吗? Thanks in advance.
提前致谢。
This ... 这个 ...
JsonPath.read(document, "$..hello.length()")
... will cast its response an instance of net.minidev.json.JSONArray
which cannot be cast to an int
. ...将其响应转换为
net.minidev.json.JSONArray
的实例,该实例无法转换为int
。 This explains the ClassCastException
这解释了
ClassCastException
The reason you see ... 你看到的原因...
[
2,
2
]
... when trying the online evaulator is that this is the toString()
for the JSONArray
instance returns that String. ...尝试在线评估程序时,这是
JSONArray
实例的toString()
返回该String的方法。
To get the "THE LENGTH OF THE NUMBER OF QUERIES " in code, just read the JsonPath output as a JSONArray
and then get the size attribute of that type. 要获取代码中的“查询长度”,只需将JsonPath输出作为
JSONArray
读取,然后获取该类型的size属性。 For example: 例如:
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());
You could also return a List<Integer
, for example: 您还可以返回
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());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.