解释如何在 Rest Assured 中获取 JSON 数组响应的大小。

rest assureddynamic programmingprogramming

我们可以在 Rest Assured 中获取 JSON 数组响应的大小。首先,我们将从请求中获取 JSON 格式的响应主体。然后将其转换为字符串。最后,使用 size 方法获取其长度。代码实现

import static io.restassured.RestAssured.given;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
public class NewTest {
   @Test
   public void jsonAryLen() {

      //从 GET 请求获取响应
      Response res = given()
      .when()
      .get("https://jsonplaceholder.typicode.com/posts");

      //将 JSON 转换为字符串
      JsonPath j = new JsonPath(res.asString());

      //JSON 数组的长度
      int s = j.getInt("data.size()");
      System.out.println( s);
   }
}

输出


相关文章