[英]RestAssured: how to extract field inside JSON array
我知道已经发布了类似的问题 ,但是答案对我没有用。
我正在使用RestAssured。 如何将我的Response
转换为JSON对象,然后提取JSON数组内的字段(以我为例,在errors
数组内)?
Response response =
given().
contentType(ContentType.JSON).
body(jsonAsMap).
when().
post("/customers").
then().
statusCode(400).extract().response();
//get "code" field inside "errors".
在Postman中测试请求时,我得到以下结果:
{
"uri": "/customers/",
"query": null,
"method": "POST",
"contentType": "application/json",
"status": 400,
"statusMessage": "Bad Request",
"errors": [
{
"field": "firstName",
"code": "firstName.required", //need to extract this field
"message": "firstName is required"
}
]
}
最简单的方法是什么?
您可以使用JsonPath访问该值并将其转换为Java类型。
像这样:
String errorCode = ...then().extract().jsonPath().getString( "errors[0].code" )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.