如何从 REstAssured 中的 Json 数组中获取 JSON 对象 [英] How to fetch JSON object from Json array in REstAssured
本文介绍了如何从 REstAssured 中的 Json 数组中获取 JSON 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
谁能帮我解决这个问题:
我是 RestAssured
的新手,并在我们的自动化脚本中处理 JSON
.我有一个 API
的响应是 JSONArray
即,
I am new to RestAssured
and handling JSON
in our automation script.
I have an API
whose response is JSONArray
i.e.,
[{
"id": 1002,
"entity": "testcase",
"fieldName": "TextName",
"displayName": "Name"
}, {
"id": 1003,
"entity": "testcase",
"fieldName": "steps",
"displayName": "TestSteps"
}]
虽然自动化,但为了验证,我需要获取响应.我尝试了下面的一个,但没有得到预期的输出
While automation, for verification i need to fetch the reponse. I have tried the below one but not getting expected output
String API = "/field/entity/testcase"
Response response = given().auth().preemptive().basic("test.manager", "test.manager").when().get(API);
JSONObject JSONResponseBody = new JSONObject(response.body().asString());
Assert.assertEquals(JSONResponseBody.getString("fieldName"), "TextName");
我也试过这个:
JSONArray array = new JSONArray();
JsonObject JSONResponseBody = array.getJsonObject(0);
感谢提前
推荐答案
你应该试试这个:
String API = "/field/entity/testcase"
Response response = given().auth().preemptive().basic("test.manager", "test.manager").when().get(API);
JSONArray JSONResponseBody = new JSONArray(response.body().asString());
Assert.assertEquals(JSONResponseBody.getJsonObject(0).getString("fieldName"), "TextName");
这篇关于如何从 REstAssured 中的 Json 数组中获取 JSON 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文