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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17020710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:44:16  来源:igfitidea点击:

total / count items from list json response

javarest-assured

提问by dileepvarma

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. please suggest.

我预计这里的总数为 2。请建议。

回答by dileepvarma

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);

expect().body("final_list.findall.size()",equalTo(2)).when().get("/list.json);

thanks

谢谢

回答by Kai Carver

if you expect final_listto 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 文档的匹配器列表中找到了这个:

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)

回答by Eyal Sooliman

Yo can do the following:

您可以执行以下操作:

jp = new JsonPath(response.asString());
String numFromsResponse = jp.get("final list.id.size()").toString();

回答by Anand Shah

You may use:

您可以使用:

var json = {
    "final list": [
        {
            "id": 123,
            "name": "sachin"
        },
        {
            "id": 234,
            "name": "sourav"
        }
    ]
}

and then

接着

json["final list"].length