Java 如何使用restasured验证响应中的值列表

Java 如何使用restasured验证响应中的值列表,java,automated-tests,rest-assured,rest-assured-jsonpath,Java,Automated Tests,Rest Assured,Rest Assured Jsonpath,有人能告诉我如何验证响应中的项目列表吗。假设响应如下所示 { "store":{ "book":[ { "author":"Nigel Rees", "category":"reference", "price":8.95, "title":"Sayings of the Century" }, {

有人能告诉我如何验证响应中的项目列表吗。假设响应如下所示

{  
   "store":{  
      "book":[  
         {  
            "author":"Nigel Rees",
            "category":"reference",
            "price":8.95,
            "title":"Sayings of the Century"
         },
         {  
            "author":"Evelyn Waugh",
            "category":"fiction",
            "price":12.99,
            "title":"Sword of Honour"
         },
         {  
            "author":"Herman Melville",
            "category":"fiction",
            "isbn":"0-553-21311-3",
            "price":8.99,
            "title":"Moby Dick"
         },
         {  
            "author":"J. R. R. Tolkien",
            "category":"fiction",
            "isbn":"0-395-19395-8",
            "price":22.99,
            "title":"The Lord of the Rings"
         }
      ]
   }
}
元素书下面有四个不同数据的列表,现在如果我想按顺序验证作者姓名和价格(例如在循环中),我该如何实现这一点


我通常将响应转换为Json文档,然后进行验证,但在本例中,如果我使用响应中的四个列表中的Json路径“Store.book.author”,它将引用哪个列表。。?这就是我的困惑所在。

在rest-assured中有一个内置方法,您可以使用它来获取作为映射列表的所有数组项

String key="book";//array key (as it mentioned in your Json)
Response response=//your API call which will return Json Object
List<Hash<String,Object>>booksList=response.jsonPath().getList(key);
//Now parse value from List
Hash<String,Object> firstBookDetails=booksList.get(0);// for first index
String author=(String)firstBookDetails.get("author");
String key=“book”//数组键(如Json中所述)
Response=//将返回Json对象的API调用
ListbooksList=response.jsonPath().getList(键);
//现在从列表中解析值
Hash firstBookDetails=booksList.get(0);//第一个索引
String author=(String)firstBookDetails.get(“author”);

在BDD中使用Rest Assured时,您可以尝试一下

given()
.when()
    .get(API URL)
.then()
     .assertThat().body("store.book.author[0]", equalTo("Nigel Rees"));

这不是四个列表,而是一个数组中的四个对象
store.book[0]。author
指第一项的作者,
store.book[1]。author
指第二项的作者,依此类推。谢谢您的回答。但是如果响应只包含对象数组(不在store或book下),我应该如何访问它。。?例如,
{“作者”:“奈杰尔·里斯”,“类别”:“参考”,“价格”:8.95,“标题”:“世纪格言”},{“作者”:“伊芙琳·沃”,“类别”:“小说”,“价格”:12.99,“标题”:“荣誉之剑”}
明白了。已使用
[0]。作者
等访问它。谢谢,这样做会更有效。