java - 放心 JsonPath : How to filter json objects from json

标签 java rest-assured rest-assured-jsonpath

我正在尝试从 json 文件中获取对象数组,但遇到了问题。

path.get("wgcTournaments.items")

我应该使用什么路径来获取项目中的所有项目(item0、item1、item2 ...)?

您能给我一些建议吗?

Json 示例

{
  "wgcTournaments": {
    "items": {
      "jcr:primaryType": "nt:unstructured",
      "item0": {
        "jcr:primaryType": "nt:unstructured",
        "test": "test",
        "test1": "test1"
      },
      "item1": {
        "jcr:primaryType": "nt:unstructured",
        "test": "test",
        "test1": "test1"
      },
      "item2": {
        "jcr:primaryType": "nt:unstructured",
        "test": "test",
        "test1": "test1"
      },
      "item3": {
        "jcr:primaryType": "nt:unstructured",
        "test": "test",
        "test1": "test1"
      }
    }
  }
}

从 items 对象中过滤 item 的最佳方法,但我不明白如何使用 json 路径来做到这一点。

最佳答案

终于我找到了我的问题的解决方案。

如果你想从项目中获取项目,你需要使用这个 json 路径

path.getObject("wgcTournaments.items*.
find{it.key.startsWith('item')}.value",ItemClass[].class);

注意: 这是 RestAssured,他使用 Gpath 更多详细信息,您可以在这里找到 http://docs.groovy-lang.org/latest/html/documentation/#_gpath

关于java - 放心 JsonPath : How to filter json objects from json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41223035/

相关文章:

java - 请放心 - 无法使用参数和正文进行 POST

java - 变量可能未初始化错误

java - 将数组设置为对象属性

java - Collections 层次结构中的 Deque 接口(interface)

rest - 如何通过RequestSpecBuilder或RequestSpecification设置放心的basePath

java - 摆脱方形护腕。或者如何获取实际的字符串

java - Spark 强制 log4j

java - 使用 rest-assured 作为通用的 http 客户端

java - 通过 RestAssured 在 JSON 中进行多个匹配断言

java - 放心 - 将 json 路径与另一个进行比较