【问题标题】:How to use 'find' in restassured to search this nested array structure?如何放心地使用“查找”来搜索这个嵌套数组结构?
【发布时间】:2021-04-12 11:38:49
【问题描述】:

放心,可以使用 JsonPath 'find' 功能来搜索对象。 https://www.javadoc.io/doc/io.rest-assured/json-path/3.0.0/io/restassured/path/json/JsonPath.html

链接中给出的示例:

{
  "store": {
    "book": [
      {
        "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
      },
      {
        "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
      },
      {
        "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99
      },
      {
        "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99
      }
    ],
    "bicycle": {
      "color": "red",
      "price": 19.95
    }
  }
}

这种搜索的一个例子是:

List<Map> books = with(Object).get("store.book.findAll { book -> book.price >= 5 && book.price <= 15 }");

我的json结构有点复杂:

我想找到 id==100770 的 json 元素(顺便说一句,它是唯一的),即位于外部数组第 5 个元素内的元素,并且是内部数组的第 3 个元素

但是我没有像上面的例子那样有明确标记的 json 路径,那么我应该在下面用什么来替换 xx?

get("xx.xx.find { xx -> xx.id == 100770 }")

谁能给点建议?

【问题讨论】:

  • 你试过了吗:List&lt;Map&gt; elements = with(Object).get("array.contents.findAll { contents-&gt; contents.id== 100770 }");

标签: rest-assured rest-assured-jsonpath gpath


【解决方案1】:

您可以在这种类型的响应遍历中选择 Groovy 路径

groovy_path = "store.book.find{it.category='reference'}.title"

您可以使用不同的响应键代替类别键和标题键来从响应中获取不同的值。

String title_of_book = response().extract().jsonPath().getString("groovy_path");

System.out.println(title_of_book); //这将打印--->世纪谚语

【讨论】:

    猜你喜欢
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多