全。我和https://github.com/json-path/JsonPath有一个非常奇怪的问题
其中一个问题似乎是实现所具有的可重入问题:当执行路径时,每个片段都返回一个字符串:
Expected to find an object with property ['ModuleListResponse'] in path $[0]['response'] but found 'java.lang.String'. This is not a json object according to the JsonProvider: 'com.jayway.jsonpath.spi.json.JsonSmartJsonProvider'.我通过将JSON对象/JSON数组传递给JsonPath.read()而不是JSON字符串来实现了这一点。在这样做之后,现在我得到了:
Filter: [0]['name'] can only be applied to arrays. Current context is: [{"startLocation":{"types":["city"],"address":"Argentina","latitude":-34.6075682,"name":"Buenos Aires","description":"Buenos Aires is the capital and largest city of Argentina. The city is located on the western shore of the estuary of the Río de la Plata, o...449a049a781"}}]如您所见,这已经是一个数组了。我已经用谷歌搜索了很多次,但找不到问题出在哪里。
关于解析它的代码,你已经知道了:
jsonString = StringUtils.trim(jsonString);
if (jsonString.startsWith("[")) {
jsonObject = new org.json.JSONArray(jsonString);
} else {
jsonObject = new JSONObject(jsonString);
}
String jsonPath = "$[0].name";
Object jsonResult = JsonPath.using(conf)
.parse(jsonObject)
.read(jsonPath);因此,问题是:为什么JsonPath将json读取为字符串,而不是json?对于第二个问题,为什么它不将其作为数组,而它显然是一个数组。
发布于 2019-12-20 00:52:39
如果将JSON格式化为易于阅读的格式,您将看到属性路径是不正确的。下面是实际的JSON:
[
{
"startLocation":
{
"types": ["city"],
"address": "Argentina",
"latitude": -34.6075682,
"name": "Buenos Aires",
"description": "Buenos Aires is the capital and largest city of Argentina. The city is located on the western shore of the estuary of the Río de la Plata, o...449a049a781"
}
}
]您正在尝试访问$.[0].name,但该值不存在。
$.[0]是
{
"startLocation": {
"types": ["city"],
"address": "Argentina",
"latitude": -34.6075682,
"name": "Buenos Aires",
"description": "Buenos Aires is the capital and largest city of Argentina. The city is located on the western shore of the estuary of the Río de la Plata, o...449a049a781"
}$.[0]的惟一顶级密钥是startLocation,所以您实际要找的是
$.[0].startLocation.name
我建议撤销你添加的JSONArray和JSONObject毛发,因为它可能掩盖了真正的问题,那只是一个无效的属性路径。
https://stackoverflow.com/questions/59376047
复制相似问题