使用foreach循环的RestAssured解析Json数组响应 [英] RestAssured Parsing Json Array Response using foreach loop

查看:148
本文介绍了使用foreach循环的RestAssured解析Json数组响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到RestAssured的响应,它是一个JsonArray,看起来与下面的代码相似

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" }]

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

我使用代码从第一个Json元素获取"id"

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
    }

    }

是否可以使用foreach代替上面的代码中的for?

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

推荐答案

而不是像这样获得根级别:

Instead of getting the root level like this:

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

您可以直接获取ID:

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

然后,您可以使用foreach循环,而不是使用像这样的索引:

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
            }
        }

最好的方法(可能)是创建表示JSON的对象. 为此,我们必须了解JSON包含什么.到目前为止,您已经具有包含JSON对象的JSON数组.每个JSON对象都包含idapplicationId.为了将此JSON解析为Java类,我们必须创建一个类.我们称之为Identity.您可以随心所欲地调用它.

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;
}

以上是JSON对象的表示形式.字段名称是JSON中的确切名称.标识符应该是公开的.

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

现在,要将JSON解析为Java类,我们可以像这样使用JsonPath:

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

基于此,您可以创建一个完整的方法,而不仅仅是像这样打印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);
    }

另一项

要使用foreach并基于id获取applicationID,您需要使用getList方法,但方式不同.

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

在上面的代码中,我们获得了JSON数组中的JSON对象的列表.

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

HashMap中的每个元素都是一个JSON对象.字符串是idapplicationId之类的属性,第二个String是每个属性的值.

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.

现在,我们可以像这样使用foreach循环来获得所需的结果:

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

这篇关于使用foreach循环的RestAssured解析Json数组响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆