RestAssured Parsing Json Array Response using foreach loop

ぃ、小莉子 提交于 2020-07-09 19:16:25

问题


I have response from RestAssured which is a JsonArray looks similar to the below code

[{ "id": "1", "applicationId": "ABC" }, { "id": "2", "applicationId": "CDE" }, { "id": "3", "applicationId": "XYZ" }]

I use code to get the "id" from the first Json element

    List<String> jresponse = response.jsonPath().getList("$");
    for (int i = 0; i < jsonResponse.size(); i++) {
    String id  = response.jsonPath().getString("id[" + i + "]");
    if(id.equals("1"){
        do something
    }
    else {
        something else
    }

    }

Is there a way to use foreach in place of for in the above code?


回答1:


Instead of getting the root level like this:

List<String> jresponse = response.jsonPath().getList("$");

You can grab IDs directly:

List<String> ids = path.getList("id");

Then, you can use foreach loop, instead of using indexes like this:

        List<String> ids = path.getList("id");
        for (String id : ids) {
            if (id.equals("1")) {
                //do something
            } else {
                //do something else
            }
        }

EDIT:

The best way (probably) is to create objects representing the JSON. In order to do that we have to understand what JSON contains. So far, you have JSON Array which contains JSON Objects. Each of JSON Object contain id and applicationId. In order to parse this JSON into a Java class we have to create a class. Let's call it Identity. You can call it whatever you want.

public class Identity {
    public String id;
    public String applicationId;
}

The above is the representation of JSON Object. Field names are exact names in JSON. Identifiers should be public.

Now, to parse JSON into Java classes we can use JsonPath like this:

Identity[] identities = path.getObject("$", Identity[].class);

Then, we iterate over the array to get what we want:

        for (Identity identity : identities) {
            if (identity.id.equals("1")) {
                System.out.println(identity.applicationId);
            }
        }

Based on that you can create a full method instead of just printing the applicationId like this:

    private static String getApplicationId(String id, Identity[] identities) {
        for (Identity identity : identities) {
            if (identity.id.equals(id)) {
                return identity.applicationId;
            }
        }
        throw new NoSuchElementException("Cannot find applicationId for id: " + id);
    }

ANOTHER EDIT:

In order to use foreach and get applicationID based on id you need to use getList method but in different manner.

List<HashMap<String, String>> responseMap = response.jsonPath().getList("$");

In the above code, we get the list of JSON Objects in the JSON Array.

Each of the elements in the HashMap is a single JSON Object. Strings are the attributes like id and applicationId and second String are the values of each attribute.

Now, we can use foreach loop like this to get desired results:

private static String getApplicationIdBasedOnId(Response response, String id) {
    List<HashMap<String, String>> responseMap = response.jsonPath().getList("$");
    for (HashMap<String, String> singleObject : responseMap) {
        if (singleObject.get("id").equals(id)) {
             return singleObject.get("applicationId");
        }
    }
    throw new NoSuchElementException("Cannot find applicationId for id: " + id);
}


来源:https://stackoverflow.com/questions/56913639/restassured-parsing-json-array-response-using-foreach-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!