如何从 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");