当前位置: 首页 > 知识库问答 >
问题:

Java json路径库的问题

董新觉
2023-03-14

全部的我和你有一个很奇怪的问题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?对于第二个问题,当它显然是一个数组时,为什么不把它作为数组。

共有1个答案

谭学名
2023-03-14

如果将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].名称,但该值不存在。

$。[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].令人震惊。名称

我建议撤消您添加的JSONArray和JSONObject fluff,因为这可能掩盖了真正的问题,这只是一个无效的属性路径。

 类似资料:
  • 本文向大家介绍Linux下动态链接库加载路径及搜索路径问题,包括了Linux下动态链接库加载路径及搜索路径问题的使用技巧和注意事项,需要的朋友参考一下 引子 近日,服务器迁移后,偷懒未重新编译nginx的,直接./nginx启动,结果遇到如下问题: “error while loading shared libraries” 这是是因为需要的动态库不在动态链接器ld.so的搜索路径导致。 ld.s

  • 问题内容: 从某人的项目中,我收到此错误: 我有最新的JDK(jdk1.6.0_25)。据我了解,我可以执行以下操作之一: 下载J2SE-1.5环境以匹配项目。 编辑项目以与最新的JDK保持一致。 我怎样才能做到这一点? 问题答案: 这是一个很好的讨论: 如何在1.5兼容模式下使用JDK 1.6 基本上你应该 下载并安装1.5 JDK。(下载链接) 在Eclipse中,转到 窗口 → 首选项 →

  • 我在设置Java开发环境时遇到了一个问题。在过去,我安装了Java,然后安装了IDE,然后开始编写代码。我现在开始设置我的笔记本电脑,使用命令行编译和记事本作为我的代码编辑器。这是我第一次尝试纠正错误。 我运行javac,它创建了我的。类文件没有问题,但是我尝试用java A运行类文件,它抛出了一个错误: c:\工作区 我的类路径设置为C:\Program Files\Java\jdk-14.0.

  • 并得到以下错误消息: 我找到了一个似乎可以解决我的问题的答案:使用Python-GeckoDriver可执行文件的Selenium需要在PATH中 但我不太明白如何操作计算机中的路径或如何以计算机工作的方式组织我的文件。

  • 问题内容: 我有两节课: MyApplication 库 该库已被编译为Library.class,并且源代码不再可用。我现在正在尝试从源代码编译MyApplication。MyApplication取决于库。该库的软件包名称为org.myCompany。我尝试将我的类路径设置为以下内容: 产生以下javac编译器错误消息: 所以我将类路径更改为: 产生完全相同的错误消息。 如何设置Windows

  • 本文向大家介绍nodejs的路径问题的解决,包括了nodejs的路径问题的解决的使用技巧和注意事项,需要的朋友参考一下 最近公司的一个开发项目,后端用的是nodejs。这两天需要打包给客户演示,就让公司一个小伙把之前3D机房的打包工具移植过来。打包之后,发现原本在开发环境下的跑的好好的项目,不能访问了。出现项目的首页不能访问的问题: can not get file index.html expr