什么是正确的方式来存储所有动态Json响应字符串的列表在放心的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
}

]
}

}

在上面的方法中,我尝试在列表中存储json响应的所有字符串,但我最终得到了这个错误">java.lang。字符串不能强制转换为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]