繁体   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");
    }
}

响应:

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

}

在上述方法中,我尝试将 Jsonresponse 的所有字符串存储在 List 中,但最终出现此错误“ java.lang.String cannot be cast to java.util.ArrayList
将所有动态字符串存储到列表的正确方法是什么?

为什么不拆分json并将响应存储在ArrayList<String>

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

您可以用逗号或任何其他字符串/字符拆分它

在这里提取country的最简单方法是:

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM