简体   繁体   English

列表json响应中的总计数/计数项

[英]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.

相关问题 java lambda:如何计算列表中的项目总数? - java lambda:How to count total items in a list? 您如何从数组列表中提取项目并对其进行计数,以便根据Java中的元素及其总计数创建映射(正确)? - How do you pull items from an array list and count them in order to create a map from the element and its total count (properly) in Java? 如何获取列表中特定索引中的项目总数 <object> ? - How to get total items count in specific index of List<object>? 从集合的所有文档中获取数组列表元素的总数 - Getting the total count of array list elements from all documents of a collection 总计数未添加到现有列表中 - Total count is not added into existing List 根据条件计算项目总数 - Count Total Number of Items based on Condition 如何在listView中初始化商品的总计数和某个价格的总价值? - How to initialize in listView total count of items and total value of some price? 将列表从 JSON 响应转换为字符串 - Convert the list to String from JSON response 从Rest Assured Json响应中获取多个项目的随机字段 - Get random field from Rest Assured Json response with multiple items 使用 Jaca JsonPath 从 JSON 响应中排除项目 - Using Jaca JsonPath to exclude items from JSON response
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM