Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rest assured 获取json数组响应的大小或长度(重新启动响应接口)_Rest Assured_Rest Assured Jsonpath - Fatal编程技术网

Rest assured 获取json数组响应的大小或长度(重新启动响应接口)

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": [ { "

我们有使用restassed的restapi自动化脚本。在此声明的响应对象中为
公共静态响应
并使用
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