java - 放心: Size of json response

标签 java json rest-assured

我是新人,放心。我已尝试使用以下代码来获取响应

@Test
    public void getData() throws IOException {
         Response response = 
                    given().
                        header("authToken",userToken).
                    when().
                        get("/students").
                    then().
                        contentType(ContentType.JSON).
                    extract().
                        response(); 
                    String jsonStr = response.getBody().asString();
                    System.out.println("Tag List************************" + jsonStr);

        }

这是 json 响应

{"max":"20","list":[
{"id":1120,"sId":1120,"sIntId":"150","type":1},
{"id":1121,"sId":1121,"sIntId":"151","type":1}
{"id":1122,"sId":1122,"sIntId":"152","type":1}
{"id":1123,"sId":1123,"sIntId":"153","type":1}
{"id":1124,"sId":1124,"sIntId":"154","type":1}]}

如何计算id 或列表的大小。帮助我。

最佳答案

您不必提取响应来验证这一点。你可以这样做:

given().
       header("authToken",userToken).
when().
       get("/students").
then().
       contentType(ContentType.JSON).
       body("list.size()", is(5));

但是如果您想从响应中提取它,您也可以:

Response response = 
given().
       header("authToken",userToken).
when().
       get("/students").
then().
       contentType(ContentType.JSON).
extract().
       response(); 
int sizeOfList = response.body().path("list.size()");

如果您只对列表的大小感兴趣,而没有其他兴趣,则可以这样做:

int sizeOfList = 
given().
       header("authToken",userToken).
when().
       get("/students").
then().
       contentType(ContentType.JSON).
extract().
       path("list.size()"); 

关于java - 放心: Size of json response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32182479/

相关文章:

java - 如何获得年月格式的时间段?

java - 在 Maven 中生成一个 Version.java 文件

java - 我们还可以访问哪些其他位置,例如 Java 中的 "user.dir"?

http-headers - 我们如何放心地传递多个 header ?

java - RestAssured - 将列表作为 QueryParam 传递

java - JPA MySQL找不到创建的表

php - 如何上传 JSON 文件以在 Javascript 运行时创建对象而不更改服务器端脚本?

javascript - 解析 JSON - 数组的 json 数组

PHP:MY​​SQL 数组到 JSON 停止重复?

java - 为单个测试用例生成 JUnit 报告