我需要从Rest Assured建模请求返回的JSON响应数据中获得两个值:
public void getCustomerStatuses() {
Response response =
given().header("X-AC-User-ID","test-user").
spec(customerApiSpec).
when().
get("/api/v6/status/" + ref + "/").
then().
assertThat().statusCode(200).extract().response();
custStatusId = response.path("$.cust[?(@.name=='STATUS_ID')].id");
custRenewalId = response.path("$.cust[?(@.name=='RENEWAL_ID')].id");
System.out.println(custStatusId);
System.out.println(custRenewalId);
}
这抛出和java.lang.IllegalArgumentException: Invalid JSON expression:Script1.groovy: 1: Unexpected input: '$.cust[?' @ line 1, column 36. $.cust[?(@.name=='STATUS_ID')].id
获得这些信息的正确、最佳方式是什么?我知道我可以将extract().response().jsonPath();
从请求中链接出去,但不确定我如何获得>1值
是的,您可以使用JsonPath
Response response =
given().header("X-AC-User-ID","test-user").
spec(customerApiSpec).
when().
get("/api/v6/status/" + ref + "/").
then().
assertThat().statusCode(200).extract().response();
JsonPath jsonPath = response.jsonPath();
如果接收到的json主体是this;
{
"value1":{
"id": 1,
"abc": {
"v1": "o1",
"v2": "o2"
}
},
"value2":{
"id": 2,
"title": "test2"
}
}
然后使用get(String path)
方法;
String v1 = jsonPath.get("value1.abc.v1"); // o1
String title = jsonPath.get("value2.title"); // test2
要求进口;
io.restassured.path.json.JsonPath;