获取json数组响应的大小或长度(restAssured Response接口)

拉1

我们有使用 RestAssured 的 REST API 自动化脚本。在这个声明的响应对象中,public static Response response;并使用 检索响应数据response.jsonPath().get("id"),在此期间甚至尝试获取 id 的大小或长度,甚至需要获取有关标签数组的详细信息。

JSON 响应:

   [
  {
    "id": 1,
    "name": "test1",
    "tags": [
      {
        "tagType": "details1",
        "tag": {
          "description": null
        }
      }
    ]
  },
  {
    "id": 2,
    "name": "test2",
    "tags": [
      {
        "tagType": "details2",
        "tag": {
          "description": null
        }
      }
    ]
  }
]

尝试了以下方法:

public static Response response;

List<String> resIDs = response.jsonPath().get("id");

System.err.println("Retrieved IDs from Response: " + resIDs);

O/P: is [1,2,3,4,5,6,7]

Tried as resIDs.size(), that also no response printed.

List<Object> size = response.jsonPath().getList("$");

System.err.println("ArraySize for IDs from Response: " + size);

要么

int size = response.jsonPath().getList("$").size();

O/P: Not printed/nothing shown

请指导如何获取尺寸/长度。

威尔弗雷德克莱门特

我似乎没有在您的代码中发现任何问题,我只是更改了一些以在本地运行并且工作正常。这是我的代码

public class S_62591968 {

    public static Response postCallWithJsonBodyParam(String URL) {
        return RestAssured.given().relaxedHTTPSValidation().contentType(ContentType.JSON).request().when().get(URL);
    }

    public static void main(String[] args) {

        String url_endPoint = "http://localhost:8089/def/abc";
        Response response = postCallWithJsonBodyParam(url_endPoint);

        List<String> resIDs = response.jsonPath().get("id");
        System.out.println("Retrieved IDs from Response : " + resIDs);
        System.out.println("ArraySize for IDs from Response : " + resIDs.size());
    }
}

安慰 :

Retrieved IDs from Response : [1, 2]
ArraySize for IDs from Response : 2

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章