Java – how to get JSON objects from the JSON array in restassured

Anyone can help me solve this problem:

I'm a novice to restassured and deal with JSON in our automated script I have an API whose response is jsonarray, that is,

[{
    "id": 1002,"entity": "testcase","fieldName": "TextName","displayName": "Name"
  },{
    "id": 1003,"fieldName": "steps","displayName": "TestSteps"
  }]

Automation, validation, I need to get a response I tried one of the following but didn't get the 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");

I've tried this too:

JSONArray array = new JSONArray();
    JsonObject JSONResponseBody = array.getJsonObject(0);

Thank you in advance

Solution

You should try this:

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");
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>