简体   繁体   English

将所有动态 Json 响应字符串存储到放心的 java 列表中的正确方法是什么?

[英]What is the correct way to store all the dynamic Json response strings to a list in restassured java?

public void getresponseString() {
    for (int i = 0; i < getInfoResp.jsonPath().getInt("Map.List.size()"); i++) {
        ArrayList<String> country = getInfoResp.jsonPath().get("Map.List[" + i + "]" + ".country");
    }
}

JsonResponse:响应:

{
"plan": {
    "program": "gtr",
    "syter": "yes"
},

    "Map": {
        "List": [
            {
                "id": "tyt6577",
                "proxy": "ENABLED",
                "type": "BENEFIT",
                "country": "us",
                "triag": null
            },
            {
                "id": "yyaqtf6327",
                "proxy": "ENABLED",
                "type": "BENEFIT",
                "country": "aus",
                "triag": null
            },
            {
                "id": "676hwjsgvhgv",
                "proxy": "ENABLED",
                "type": "BENEFIT",
                "country": "rus",
                "triag": null
            },
           {
                "id": "676hsdhgv",
                "proxy": "ENABLED",
                "type": "BENEFIT",
                "country": "spa",
                "triag": null
            },
           {
                "id": "623ujhhgv",
                "proxy": "ENABLED",
                "type": "BENEFIT",
                "country": "cha",
                "triag": null
            }
           
        ]
    }

} }

In the above method, I try to store all the strings of Jsonresponse in List but I got this error finally " java.lang.String cannot be cast to java.util.ArrayList "在上述方法中,我尝试将 Jsonresponse 的所有字符串存储在 List 中,但最终出现此错误“ java.lang.String cannot be cast to java.util.ArrayList
What is the correct way to store all the dynamic strings to list?将所有动态字符串存储到列表的正确方法是什么?

Why don't you split the json and store the response in the ArrayList<String>为什么不拆分json并将响应存储在ArrayList<String>

ArrayList<String> list = new ArrayList<>(Arrays.stream(response.split(",")).collect(Collectors.toList()));

You can split it either by comma or any other String/Char您可以用逗号或任何其他字符串/字符拆分它

The easiest way to extract country here is:在这里提取country的最简单方法是:

ArrayList<String> country = getInfoResp.jsonPath().get("Map.List.country");
//[us, aus, rus, spa, cha]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 验证所有响应数据的最佳方法是什么? - What is the best way to verify the all response data in restassured? 根据RESTAssured / Java中的JSON响应验证UI - Validating UI against JSON response in RESTAssured/Java 有没有办法通过RestAssured Java断言响应主体在Json中需要参数 - Is there a way to assert that response body has required parameters in Json through RestAssured Java 在java中等待客户端响应的正确方法是什么? - What is the correct way to wait for a client response in java? 在 Java 中构建 JSON 字符串的正确方法是什么,这样您就不会在结果中得到额外的斜杠字符? - What is the correct way to build JSON strings in Java so you don't get extra slash characters in result? 初始化 java 中的字符串列表的最短方法是什么? - What is the shortest way to initialize List of strings in java? 在 vert.x 中处理大型 json 字符串的正确方法是什么? - What is the correct way to handle large json strings in vert.x? 泽比爪哇。 将json存储在变量中的正确方法 - Zeebe Java. Correct way to store json in variable 将本机指针存储在Java对象中的“正确”方法是什么? - What is the 'correct' way to store a native pointer inside a Java object? 在 Java 中存储字符串的公共部分的最佳方法是什么? - What is the best way to store a common part of Strings in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM