如何从java/android中的嵌套json对象访问数据?

v440hwme  于 2021-07-12  发布在  Java
关注(0)|答案(2)|浏览(371)

这个问题在这里已经有答案了

如何在android中解析json[重复](3个答案)
两天前关门了。

{
        "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"
            }
}

这是我的JSONAPI,我如何访问来自“区域”的数据以及来自“竞赛”的数据。

我可以访问“竞赛”的数据,下面是我的代码:

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");
}

现在我想从“区域”访问数据。谢谢您

v1l68za4

v1l68za41#

您可以从中创建一个新的json对象 jitems :

for (int i = 0; i < jsonArray.length(); i++) {
    JSONObject jitems = jsonArray.getJSONObject(i);
    JSONObject area = jitems.getJSONObject("area");
    String name = area.getString("name");
    ...
0sgqnhkj

0sgqnhkj2#

您可以按以下方式访问

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();
    }

相关问题