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

Get size or length of json array response (restAssured Response interface)

我们有使用 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

请指导如何获得size/length。

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

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