将 REST API 请求 json 值提取到 RestAssured 中的变量
Extract REST API request json value to variable in RestAssured
我们必须验证在我们的 REST API 请求负载中发送的值和响应值,尝试了下面的方法并能够打印 JSON 数组。如何获取请求体数组中的特定对象JSON body.
请求负载:
{
"Testinfo":{
"abc":2,
"xyz":"2020-01-01"
},
"Details":{
"eductation":{
"test1":9,
"test2":100,
"test3":50
},
"neweductiona":{
"test1":"value",
"test2":"Draws"
}
}
}
代码:
jsonObject = (JSONObject) JSONValue.parse(request);
JSONObject Testinfo = (JSONObject) jsonObject.get("Testinfo");
JSONObject Details = (JSONObject) jsonObject.get("Details");
System.err.println("Testinfo value in the request is: " + Testinfo);
System.err.println("Details value in the request is: " + Details);
输出:
{
"abc":2,
"xyz":"2020-01-01"
},
{
"eductation":{
"test1":9,
"test2":100,
"test3":50
},
"neweductiona":{
"test1":"value",
"test2":"Draws",
}
如何获取特定值如"Details.eductation.test3"
试过以下
JSONArray jsonArray = (JSONArray) JSONValue.parse(request)
但出现错误:
java.lang.ClassCastException: net.minidev.json.JSONObject cannot be cast to net.minidev.json.JSONArray
请指导。
使用 JSONPath
String json = "{\r\n" + " \"Testinfo\": {\r\n" + " \"abc\": 2,\r\n"
+ " \"xyz\": \"2020-01-01\"\r\n" + "\r\n" + "\r\n" + " },\r\n" + "\r\n" + " \"Details\": {\r\n"
+ " \"eductation\": {\r\n" + " \"test1\": 9,\r\n" + " \"test2\": 100,\r\n"
+ " \"test3\": 50\r\n" + " },\r\n" + " \"neweductiona\": {\r\n"
+ " \"test1\": \"value\",\r\n" + " \"test2\": \"Draws\",\r\n"
+ " \"test3\": 50\r\n" + "\r\n" + " }\r\n" + " }\r\n" + "}";
JsonPath js = new JsonPath(json);
System.out.println("Value is : "+js.getString("Details.eductation.test3"));
输出:
Value is : 50
我们必须验证在我们的 REST API 请求负载中发送的值和响应值,尝试了下面的方法并能够打印 JSON 数组。如何获取请求体数组中的特定对象JSON body.
请求负载:
{
"Testinfo":{
"abc":2,
"xyz":"2020-01-01"
},
"Details":{
"eductation":{
"test1":9,
"test2":100,
"test3":50
},
"neweductiona":{
"test1":"value",
"test2":"Draws"
}
}
}
代码:
jsonObject = (JSONObject) JSONValue.parse(request);
JSONObject Testinfo = (JSONObject) jsonObject.get("Testinfo");
JSONObject Details = (JSONObject) jsonObject.get("Details");
System.err.println("Testinfo value in the request is: " + Testinfo);
System.err.println("Details value in the request is: " + Details);
输出:
{
"abc":2,
"xyz":"2020-01-01"
},
{
"eductation":{
"test1":9,
"test2":100,
"test3":50
},
"neweductiona":{
"test1":"value",
"test2":"Draws",
}
如何获取特定值如"Details.eductation.test3"
试过以下
JSONArray jsonArray = (JSONArray) JSONValue.parse(request)
但出现错误:
java.lang.ClassCastException: net.minidev.json.JSONObject cannot be cast to net.minidev.json.JSONArray
请指导。
使用 JSONPath
String json = "{\r\n" + " \"Testinfo\": {\r\n" + " \"abc\": 2,\r\n"
+ " \"xyz\": \"2020-01-01\"\r\n" + "\r\n" + "\r\n" + " },\r\n" + "\r\n" + " \"Details\": {\r\n"
+ " \"eductation\": {\r\n" + " \"test1\": 9,\r\n" + " \"test2\": 100,\r\n"
+ " \"test3\": 50\r\n" + " },\r\n" + " \"neweductiona\": {\r\n"
+ " \"test1\": \"value\",\r\n" + " \"test2\": \"Draws\",\r\n"
+ " \"test3\": 50\r\n" + "\r\n" + " }\r\n" + " }\r\n" + "}";
JsonPath js = new JsonPath(json);
System.out.println("Value is : "+js.getString("Details.eductation.test3"));
输出:
Value is : 50