Home > Software engineering >  What is the correct way to store all the dynamic Json response strings to a list in restassured java
What is the correct way to store all the dynamic Json response strings to a list in restassured java

Time:07-04

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"
What is the correct way to store all the dynamic strings to list?

CodePudding user response:

Why don't you split the json and store the response in the 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

CodePudding user response:

The easiest way to extract country here is:

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