Rest assured 获取json数组响应的大小或长度(重新启动响应接口)
我们有使用restassed的restapi自动化脚本。在此声明的响应对象中为Rest assured 获取json数组响应的大小或长度(重新启动响应接口),rest-assured,rest-assured-jsonpath,Rest Assured,Rest Assured Jsonpath,我们有使用restassed的restapi自动化脚本。在此声明的响应对象中为公共静态响应并使用response.jsonPath().get(“id”)检索响应数据,在此过程中,甚至尝试获取id的大小或长度,甚至需要获取关于标记数组的详细信息 JSON响应: [ { "id": 1, "name": "test1", "tags": [ { "
公共静态响应
并使用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
}
}
]
}
]
尝试以下方法:
公共静态响应代码>
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);
请指导如何获取大小/长度。我似乎在您的代码中没有发现任何问题,我只是更改了一点以在本地运行,并且工作正常。这是我的密码
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());
}
}
因此,您只需要将输出设置为7?感谢您的回复和扩展帮助,是的,您还希望获得嵌套数组“tags”的大小详细信息。resIDs.size()
为我仍然无法获取的meI返回正确的计数,这是尝试过的方法:List resIDs=response.jsonPath().get(“id”);System.err.println(“响应中ID的数组化:“+resIDs.size()”);这正是它应该的样子,看看我的输出
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