放心,可以使用 JsonPath 的“查找”功能来搜索对象。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 }")
有人可以提出任何建议吗?