gpt4 book ai didi

java - Java json-path 库的问题

转载 作者:行者123 更新时间:2023-12-01 19:19:31 24 4
gpt4 key购买 nike

全部。我遇到了一个非常奇怪的问题 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'.

我通过将 JSONObject/JSONArray 传递给 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?对于第二个问题,当它显然是一个数组时,为什么不将其作为数组。

最佳答案

如果将 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 绒毛,因为它可能掩盖了真正的问题,这只是一个无效的属性路径。

关于java - Java json-path 库的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59376047/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com