使用带有 Java 的 RESTassured 库列出节点中的参数

Listing the parameters in a node using RESTassured library with Java

Web 服务:http://services.groupkt.com/country/search?text=lands 从 GET 请求返回了 16 条记录。在记录中,有一个参数叫做'name'。因此,有 16 个名称,每个名称都有一个唯一值(国家/地区)。我的意图是使用 java 和 RESTassured 库列出 'name' 参数的所有 16 个值。我试过这个:

Response response = RestAssured.get("http://services.groupkt.com/country/search?text=lands").andReturn();
String json = response.getBody().asString();
JsonPath jp = new JsonPath(json);

List<String> ls = from(response).getList("RestResponse.result.name");// The 'from' text displays an error

在 'from' 文本中看到一个错误,它说:RestTest 类型中的方法 from(String) 不适用于参数 (Response)。我不确定如何纠正这个问题。有没有一种简单的方法来创建 'name' 参数的所有值的列表?

尝试将 from(response) 替换为 from(jp)。那应该解决您遇到的错误。

响应 class(get() 方法 returns)也支持 jsonPath,因此您也可以将其重构为如下内容:

List names= get("http://services.groupkt.com/country/search?text=lands").jsonPath().getList("RestResponse.result.name");