在通过 RestAssured API 调用后,我从 REST API 获得了响应到 Response 对象。
响应主体是json,我想从中获取特定的键值?
代码如下
Response res = given()
.relaxedHTTPSValidation()
.with()
.contentType(ConfigReader.get("application.json"))
.then()
.get(url);
String rbody = res.body().asString();
如何获取rbody
字符串中的特定键值?
最佳答案
Response
类有方法 path()
使用它,用户可以提供 json 路径以获取特定值。
例如:-
Response res = given()
.relaxedHTTPSValidation()
.with()
.contentType(ConfigReader.get("application.json"))
.then()
.get(url);
String value = res.path("root.childKey").toString();
root.childKey
为json元素的路径
关于rest-assured - 如何从响应对象中获取特定的 json 键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43730536/