问题描述
放心,可以使用 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
},"author": "Herman Melville","title": "Moby Dick","isbn": "0-553-21311-3","price": 8.99
},"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 }");
我想找到 id==100770 的 json 元素(顺便说一句,它是唯一的),即位于外部数组第 5 个元素内的元素,并且是内部数组的第 3 个元素
但是我没有像上面的例子那样明确标记的 json 路径,那么我应该在下面使用什么来替换 xx?
get("xx.xx.find { xx -> xx.id == 100770 }")
谁能给点建议?
解决方法
您可以在这种类型的响应遍历中使用 Groovy 路径
groovy_path = "store.book.find{it.category='reference'}.title"
您可以使用不同的响应键代替类别键和标题键来从响应中获取不同的值。
String titile_of_book = response().extract().jsonPath().getString("groovy_path");
System.out.println(title_of_book); //这将打印--->世纪语录