我需要在获取请求时以JSON格式获取数据,数据应采用以下格式:
{
"birth_date": "1980-01-01",
"birth_place": "",
"sex": "",
"inn": "",
"snils": "",
"rezident": "0",
"depend_count": null,
"name": {
"surname": "Заноза",
"name": "Заноза",
"patronymic": "Заноза"
}
}
但是数据以以下形式返回,以字符串形式响应:
{
"birth_date" : "1980-01-01"
,
"birth_place" : ""
,
"sex" : ""
,
"inn" : ""
,
"snils" : ""
,
"rezident" : "0"
,
"depend_count" : null,
"name" : {
"surname" : "\u0417\u0430\u043D\u043E\u0437\u0430"
,
"name" : "\u0417\u0430\u043D\u043E\u0437\u0430"
,
"patronymic" : "\u0417\u0430\u043D\u043E\u0437\u0430"
}
}
代码:
public static String getClientInfo(String clientId) {
String response = given().
param("id", clientId).
when().get("/services/clients").
then().assertThat().statusCode(200).and().
extract().body().asString();
System.out.println("Response = " + response);
return response;
}
如何使用Rest-Assured获取请求时获取JSON格式?
答案 0 :(得分:0)
以下代码段可能会对您有所帮助。
public static String getClientInfo(String clientId) {
Response response = given().
param("id", clientId).
when().get("/services/clients");
response.then().assertThat().statusCode(200);
System.out.println("Non pretty Response : " + response.body().asString());
System.out.println("Pretty Response : " + response.prettyPrint().toString());
return response.prettyPrint().toString().replace("\\", "");
}