Shehab ElDin Shehab ElDin - 1 month ago 6
JSON Question

get ArrayList from Json

I have a Json response from REST API like this:

{
"data": {
"leagues": [
{
"identifier": "8e7fa444c4b60383727fb61fcc6aa387",
"league_slug": "bundesliga",
"name": "Bundesliga",
"nation": "Germany",
"level": "1",
"federation": "UEFA"
},
{
"identifier": "726a53a8c50d6c7a66fe0ab16bdf9bb1",
"league_slug": "premier-league",
"name": "Premier League",
"nation": "England",
"level": "1",
"federation": "UEFA"
},
{
"identifier": "bc425f51d5ee924580c35c38da138de8",
"league_slug": "serie-a",
"name": "Serie A",
"nation": "Italy",
"level": "1",
"federation": "UEFA"
}
],
"statusCode": "200",
"errorCode": "0",
"statusReason": "OK"
}
}


I want to get List of Leagues from this Json
now I have "data" hashmap but I can't get array of hashMaps of Leagues.
I don't want to convert it to any data type, I only want
ArrayList<Map<String, String>>


please help me to get this information using
Gson
or
JSONObject

Answer

You can parse this string using

JSONObject json = new JSONObject(str); 

When you can retrive any data you want:

JSONObject data = json.getJSONObject("data");
JSONArray leagues = data.getJSONArray("leagues");

iterate like this:

for (int i = 0; i < leagues.length(); i++) {
     JSONObject childJSONObject = leagues.getJSONObject(i);
}
Comments