[英]How to get Complex JSON response into Array List? What to write in "on response" to get only records from json response?
[英]Get all records from JSON response
我对Rest Assured API世界还是很陌生。 我已经阅读了尽可能多的https://github.com/rest-assured/rest-assured/wiki/Usage#example-3---complex-parsing-and-validation上的文档。
我有一个类似的回复:
{
"StatusCode": 200,
"Result": [
{
"EmployeeId": "5661631",
"PhoneTypeDescription": "Home",
"PhoneNumber": "9701234567",
},
{
"EmployeeId": "5661631",
"PhoneTypeDescription": "mobile1",
"PhoneNumber": "2531234567",
},
{
"EmployeeId": "5661631",
"PhoneTypeDescription": "mobile2",
"PhoneNumber": "8081234567",
}
]
}
我一直在努力获取仅第一条记录的电话号码。
String responseBody=
given()
.relaxedHTTPSValidation().contentType("application/json")
.param("api_key", api_key).
when()
.get("/api/employees/" + employeeId)
.andReturn().asString();
JsonPath jsonPath = new JsonPath(responseBody).setRoot("Result");
phoneNumber = jsonPath.getString("PhoneNumber");
在这种情况下,我得到了所有电话号码:phoneNumber =“ [9701234567,2531234567,8081234567]”
我怎样才能只获得第一张唱片? 我宁愿不必执行字符串操作来处理“ [”。
谢谢
你可以做,
JSONObject json = new JSONObject(responseBody);
phoneNumber = json.getJSONArray("Result").getJSONObject(0).getString("PhoneNumber");
此处, 0
表示JSON Array Result
的第一条记录。
因为您知道要检索的元素的索引,所以可以使用以下代码:
JsonPath jsonPath = new JsonPath(response);
String phoneNumber = jsonPath.getString("Result[0].PhoneNumber");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.