簡體   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