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