我是新人,放心。我已尝试使用以下代码来获取响应
@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/