简体   繁体   English

如何在json响应中获取数组的大小? 放心API、JAVA

[英]How get size of an array in json response? rest assured API, JAVA

I found a lot of topics with this question on this portal, but anyway I was not able to achieve what I need.我在这个门户网站上找到了很多关于这个问题的主题,但无论如何我都无法实现我所需要的。 I keep getting the next exception: java.lang.IllegalArgumentException: The parameter "roles" was used but not defined. Define parameters using the JsonPath.params(...) function我不断收到下一个异常: java.lang.IllegalArgumentException: The parameter "roles" was used but not defined. Define parameters using the JsonPath.params(...) function java.lang.IllegalArgumentException: The parameter "roles" was used but not defined. Define parameters using the JsonPath.params(...) function . java.lang.IllegalArgumentException: The parameter "roles" was used but not defined. Define parameters using the JsonPath.params(...) function

Partially my code:部分我的代码:

import static io.restassured.RestAssured.given;
import io.restassured.RestAssured;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;


String baseURItest = RestAssured.baseURI = "http://testapi.test.com/testapps");
Response response;
response = given().when().get("/getAllRoles?token=token");
countRoles=response.body().path("$..roles.size()");
System.out.println(countRoles);

console output:控制台输出:

> java.lang.IllegalArgumentException: The parameter "roles" was used but
> not defined. Define parameters using the JsonPath.params(...) function

and json body response:和 json 正文响应:

{
    "Message": "",
    "Status": 200,
    "Data": {
        "errors": [],
        "roles": [
            {
                "ROLEKEY": "1",
                "ROLEID": 1,
                "ROLENAME": "name1",
                "ROLEDESCRIPTION": "1"
            },
            {
                "ROLEKEY": "2",
                "ROLEID": 2,
                "ROLENAME": "name2",
                "ROLEDESCRIPTION": "12"
            },
            {
                "ROLEKEY": "3",
                "ROLEID": 3,
                "ROLENAME": "name3",
                "ROLEDESCRIPTION": "x"
            }
        ]
    }
}

I also tried :我也试过:

 JsonPath jsonPathValidator = response.jsonPath(); System.out.println(jsonPathValidator.get("$..ROLEKEY").toString());

I would say I tried a lot of different ways that I found in google, but each time I will get the same error.我会说我尝试了很多在 google 中找到的不同方法,但每次都会遇到相同的错误。 Can someone please explain to me what am I missing here, please?有人可以向我解释我在这里缺少什么吗? Or what should I do?或者我该怎么办? Thank you in advance for any help!预先感谢您的任何帮助!

Problem: You misunderstand JsonPath in Rest-Assured.问题:您误解了 Rest-Assured 中的 JsonPath。

Note from Rest-Assured wiki .来自 Rest-Assured wiki 的注释。

Note that the "json path" syntax uses Groovy's GPath notation and is not to be confused with Jayway's JsonPath syntax.请注意,“json 路径”语法使用 Groovy 的 GPath 表示法,不要与 Jayway 的 JsonPath 语法混淆。

Solution: you can choose one of two ways below:解决方法:您可以选择以下两种方式之一:

  1. Use JsonPath jayway by adding this to pom.xml (or gradle.build)通过将其添加到 pom.xml(或 gradle.build)来使用 JsonPath jayway
<dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    <version>2.6.0</version>
</dependency>
Response res = given().when().get("/getAllRoles?token=token");
JsonPath.read(res.asString(), "$..ROLEKEY");
  1. You can use JsonPath from Rest-Assured.您可以使用 Rest-Assured 的 JsonPath。
List<String> roles = response.jsonPath().getList("Data.roles.ROLEKEY");
System.out.println(roles);
//[1,2,3]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM