【问题标题】:How can i access data from nested json objects in java / android? [duplicate]如何从 java / android 中的嵌套 json 对象访问数据? [复制]
【发布时间】:2021-07-17 06:50:30
【问题描述】:
    {
        "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"
            }
}

这是我的 JSON API,我如何访问“区域”中的数据以及“比赛”中的数据。


#i 能够从“竞赛”访问数据,下面是我的代码:

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

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

【问题讨论】:

  • 使用 Gson 将 JSON 映射到对其建模的 Java 对象上,而不是尝试手动进行所有提取。
  • 帮助..请发布一些代码片段

标签: java android json api


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    您可以通过以下方式访问

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

    【讨论】:

      猜你喜欢
      • 2015-08-13
      • 2017-11-27
      • 1970-01-01
      • 2021-10-24
      • 1970-01-01
      • 2017-12-26
      • 2013-10-12
      • 2018-08-28
      • 2020-08-09
      相关资源
      最近更新 更多