如何从响应对象中获取特定的 json 键值
How to get particular json key value from Response Object
在通过 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
字符串中的特定键值?
JSON 的格式类似于 {someProprty:"someValue"},因此您需要访问特定的 属性,而不是将其作为字符串获取。即:b.body.someProperty
注意:我强烈建议您将您的回复命名为 res
或 response
。你不会喜欢 b
作为你的回应。
How to access JSON Object name/value?
JSON 也可以格式化为 {somePropertyThatIsNumerical:1} 或者可以包含数组。
Response
class 有方法 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元素的路径
作为 Restassured 一部分的 JasonPath class 是我在我的项目中使用的那个。首先,您需要导入 JsonPath class 使用:
import com.jayway.restassured.path.json.JsonPath;
然后您需要传递 JSON 字符串并使用它来创建 JsonPath 对象。您可以使用键从 JsonPath 对象中获取相应的值。以下代码将为您工作。
Response res = given()
.relaxedHTTPSValidation()
.with()
.contentType(ConfigReader.get("application.json"))
.then()
.get(url);
String rbody = res.asString();
JsonPath jp = new JsonPath( rbody );
String value = jp.getString( "your.key" );
baseURI="url";
Map<String,String> reqParam=new HashMap<String,String>();
reqParam.put("loginID","abc");
reqParam.put("password","123");
JSONObject reqObjects=new JSONObject(reqParam);
Response response =
given()
.header("Content-Type", "application/json").accept(ContentType.JSON)
.when()
.body(reqObjects.toJSONString()).post("/v1/getDetails")
.then().log().body().extract().response();
String responseBody= response.asString();
JsonPath path=new JsonPath(responseBody);
String key=path.getString("path.key");
在通过 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
字符串中的特定键值?
JSON 的格式类似于 {someProprty:"someValue"},因此您需要访问特定的 属性,而不是将其作为字符串获取。即:b.body.someProperty
注意:我强烈建议您将您的回复命名为 res
或 response
。你不会喜欢 b
作为你的回应。
How to access JSON Object name/value?
JSON 也可以格式化为 {somePropertyThatIsNumerical:1} 或者可以包含数组。
Response
class 有方法 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元素的路径
作为 Restassured 一部分的 JasonPath class 是我在我的项目中使用的那个。首先,您需要导入 JsonPath class 使用:
import com.jayway.restassured.path.json.JsonPath;
然后您需要传递 JSON 字符串并使用它来创建 JsonPath 对象。您可以使用键从 JsonPath 对象中获取相应的值。以下代码将为您工作。
Response res = given()
.relaxedHTTPSValidation()
.with()
.contentType(ConfigReader.get("application.json"))
.then()
.get(url);
String rbody = res.asString();
JsonPath jp = new JsonPath( rbody );
String value = jp.getString( "your.key" );
baseURI="url";
Map<String,String> reqParam=new HashMap<String,String>();
reqParam.put("loginID","abc");
reqParam.put("password","123");
JSONObject reqObjects=new JSONObject(reqParam);
Response response =
given()
.header("Content-Type", "application/json").accept(ContentType.JSON)
.when()
.body(reqObjects.toJSONString()).post("/v1/getDetails")
.then().log().body().extract().response();
String responseBody= response.asString();
JsonPath path=new JsonPath(responseBody);
String key=path.getString("path.key");