-1
    {
        "count": 152,
        "filters": {},
        "competitions": [
            {
                "id": 2006,
                "area": {
                    "id": 2001,
                    "name": "Africa",
                    "countryCode": "AFR",
                    "ensignUrl": null
                },
                "name": "WC Qualification",
                "code": null,
                "emblemUrl": null,
                "plan": "TIER_FOUR",
                "currentSeason": {
                    "id": 555,
                    "startDate": "2019-09-04",
                    "endDate": "2021-11-16",
                    "currentMatchday": 1,
                    "winner": null
                },
                "numberOfAvailableSeasons": 2,
                "lastUpdated": "2018-06-04T23:54:04Z"
            }
}

this is my JSON API and how can i access data from "area" along with data from "competitions".


#i was able to access data from "competitions" and below is my code:

 JSONObject jsonObject = new JSONObject(responseValue);
                            JSONArray jsonArray = jsonObject.getJSONArray("competitions");
                            competitionsModelList = new ArrayList<>();
    
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject jitems = jsonArray.getJSONObject(i);
                                name = jitems.getString("name");
                                id = jitems.getInt("id");
                                code = jitems.getString("code");
                                emblemUrl = jitems.getString("emblemUrl");
                                plan = jitems.getString("plan");
                                numberOfSeasonAvailable = items.getInt("numberOfAvailableSeasons");
                                lastUpdated = jitems.getString("lastUpdated");
}

Now i want to access data from "area". thank you

Rudra Rokaya
  • 637
  • 1
  • 5
  • 9

2 Answers2

0

You can just create a new JSON object from jitems:

for (int i = 0; i < jsonArray.length(); i++) {
    JSONObject jitems = jsonArray.getJSONObject(i);
    JSONObject area = jitems.getJSONObject("area");
    String name = area.getString("name");
    ...
Zain
  • 37,492
  • 7
  • 60
  • 84
0

You can access as below

try
    {
        JSONObject jsonObject = new JSONObject(responseValue);
        JSONArray jsonArray = jsonObject.getJSONArray("competitions");
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jitems = jsonArray.getJSONObject(i);

            id = jitems.getInt("id");
            name = jitems.getString("name");
            code = jitems.getString("code");
            emblemUrl = jitems.getString("emblemUrl");
            plan = jitems.getString("plan");
            numberOfSeasonAvailable = jitems.getInt("numberOfAvailableSeasons");
            lastUpdated = jitems.getString("lastUpdated");

            JSONObject jitemsArea = jitems.getJSONObject("area");
            areaId = jitemsArea.getInt("id");
            areaName = jitemsArea.getString("name");
            areaCountryCode = jitemsArea.getString("countryCode");


        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31