java 如何从 REstAssured 中的 Json 数组中获取 JSON 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35263637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 23:55:31  来源:igfitidea点击:

How to fetch JSON object from Json array in REstAssured

javarestrest-assured

提问by Kavana

Can anyone please help me to solve this scenario:

谁能帮我解决这个问题:

I am new to RestAssuredand handling JSONin our automation script. I have an APIwhose response is JSONArrayi.e.,

我是我们的自动化脚本的新手RestAssured和处理者JSON。我有一个API他的回应是JSONArray

  [{
    "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");

and also i tried with this:

我也试过这个:

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

Thanks Inadvance

提前致谢

采纳答案by Kruti Patel

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");

回答by Anoop Philip

These kind of validation can achieve directly using restAssured - ValidatableResponseOptions itself

这种验证可以直接使用restAssured——ValidatableResponseOptions本身来实现

    String API = "/field/entity/testcase"
    given().auth().preemptive().basic("test.manager", "test.manager").
    when().get(API).
    then().assertThat().body("fieldName[0]", equalTo("TextName");

Note - "equalTo" validation needs following static import

注意 - 静态导入后需要“equalTo”验证

import static org.hamcrest.Matchers.equalTo;

回答by djangofan

Just as another idea of how to do this, I would do it maybe like this instead:

就像如何做到这一点的另一个想法一样,我可能会这样做:

ValidatableResponse statusResponse = givenJsonRequest().when()
    .get("/field/entity/test").then();
ArrayList<Map<String,?>> jsonAsArrayList = statusResponse.extract()
    .jsonPath().get("");
Optional<Map<String,?>> filtered = jsonAsArrayList.stream()
    .filter(m -> m.get("fieldName1").equals("Whatever1"))
    .filter(m -> m.get("jsonObject").toString().contains("Whatever2"))
    .findFirst();
Assert.assertTrue(filtered.isPresent(), "Test expected a result after filtering.");