如何从 Rest Assured 中的嵌套列表中获取值?
我们可以在 Rest Assured 中从嵌套列表中获取值。通过使用 extract 方法来完成。要获取该项,我们必须使用 path 方法(在 extract 方法后)并在响应中传递我们希望获得的项。
我们首先将通过 Postman 在一个模拟的 API URL 上发送一个 GET 请求,并遍历其具有嵌套列表的响应。

范例
代码实现
import org.testng.annotations.Test;
import static io.restassured.RestAssured.given;
import java.util.ArrayList;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
public class NewTest {
@Test
public void getRequest() {
//base URL
RestAssured.baseURI = "https://run.mocky.io/v3";
RestAssured.basePath =
"/23ab8486-7d8d-41e4-be27-d603c767d745";
//response from GET request
given()
.when().get().prettyPrint();
//extract values from Response
String name = given().contentType(ContentType.JSON).get()
.then().extract().path("name");
System.out.println(name);
String age = given().contentType(ContentType.JSON).get()
.then().extract().path("age");
System.out.println(age);
//extract values from a nested list in Response
ArrayList<String> s = given().contentType(ContentType.JSON).get()
.then().extract().path("subjects");
for(String subject: s) {
System.out.println(subject);
}
}
}输出

广告
数据结构
联网
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP