[英]total / count items from list json response
im trying to get count / total number of records from list api using Restassured. 我试图使用Restassured从列表api获取计数/总记录数。
Please share thoughts on how can we get it. 请分享我们如何获得它的想法。
ex: 例如:
{
"final list": [
{
"id": 123,
"name": "sachin"
},
{
"id": 234,
"name": "sourav"
}
]
}
I am expecting total as 2 here. 我期待这里的总数为2。 please suggest.
请建议。
I got answer to my question. 我得到了回答我的问题。
here is the code for rest assured 这是放心的代码
expect().body("final_list.findall.size()",equalTo(2)).when().get("/list.json); 期待()体( “final_list.findall.size()”,equalTo(2))当()获得(“/ list.json)。;
thanks 谢谢
if you expect final_list
to have 2 elements, you can use the hasSize()
matcher: 如果你希望
final_list
有2个元素,你可以使用hasSize()
匹配器:
expect().body("final_list", hasSize(2)).when().get("/list.json);
I found this in the list of matchers from the Matchers class Java doc: 我在Matchers类Java doc的匹配器列表中找到了这个:
http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matchers.html#hasSize(org.hamcrest.Matcher) http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matchers.html#hasSize(org.hamcrest.Matcher)
Yo can do the following: 哟可以做到以下几点:
jp = new JsonPath(response.asString());
String numFromsResponse = jp.get("final list.id.size()").toString();
You may use: 你可以使用:
var json = {
"final list": [
{
"id": 123,
"name": "sachin"
},
{
"id": 234,
"name": "sourav"
}
]
}
and then 接着
json["final list"].length
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.