【问题标题】:sorting nested arrays in java在java中对嵌套数组进行排序
【发布时间】:2019-09-10 14:37:01
【问题描述】:

如何从这个嵌套数组中整理出以下信息。我在字符 2 第 1 行收到 JSONException。数据、代码和错误在下面。

  1. localteam_id
  2. Visitorsteam_id 3.Localteam_score
  3. Visitorsteam_score
  4. 时间
  5. 访客组名
               {
            "data":[
            {
            "id": 11903988,
            "league_id": 498,
            "season_id": 16357,
         "stage_id": 77445097,
        "round_id": 176699,
         "group_id": null,
         "aggregate_id": null,
         "venue_id": null,
         "referee_id": null,
         "localteam_id": 237692,
         "visitorteam_id": 237702,
         "winner_team_id": null,
        "weather_report": null,
        "commentaries": false,
         "attendance": null,
          "pitch": null,
        "details": null,
          "neutral_venue": false,
         "winning_odds_calculated": false,
         "formations":{"localteam_formation": null, "visitorteam_formation": 
      null},
         "scores":{
         "localteam_score": 0,
         "visitorteam_score": 0,
         "localteam_pen_score": null,
         "visitorteam_pen_score": null,
         "ht_score": null,
         "ft_score": null,
         "et_score": null
         },
         "time":{"status": "TBA", "starting_at":{"date_time": "2019-09-14 
           00:00:00", "date": "2019-09-14",…},
         "coaches":{
         "localteam_coach_id": null,
         "visitorteam_coach_id": null
          },
         "standings":{
         "localteam_position": 12,
         "visitorteam_position": 4
         },
         "assistants":{"first_assistant_id": null, "second_assistant_id": 
  null, 
         "fourth_official_id": null},
       "leg": "1/1",
       "colors": null,
       "deleted": false,
       "localTeam":{"data":{"id": 237692, "legacy_id": null, "name": "Sochi 
     U20", 
         "short_code": null,…},
         "visitorTeam":{
         "data":{
         "id": 237702,
         "legacy_id": null,
         "name": "Lokomotiv Moskva U20",
         "short_code": null,
         "twitter": null,
         "country_id": 227,
         "national_team": false,
           "founded": null,
         "logo_path": null,
         "venue_id": null,
         "current_season_id": 16357
         }
      }
        ]
         }

我的代码:

JSONObject objj = parentArray.getJSONObject(i); 
league_id=objj.getString("data");
objj.league_id.[0];

错误:

org.json.JSONException: JSONArray 文本必须以 '[' 在 1 [字符 2 第 1 行] 开始

【问题讨论】:

  • 什么是parentArray?你确实意识到你的 JSON 不是一个数组,对吧?它是一个包含一个名为"data" 的数组的对象。
  • JSONArray parentArray = new JSONArray(finaljson);它是 JSON 对象的实例

标签: java arrays json nested


【解决方案1】:

问题出在

JSONArray parentArray = new JSONArray(finaljson);

您正在尝试将一个对象(即整个 json)读取到一个数组中。

该数组是您父级“JSONObject”中的“数据”

要获取league_id,您的代码应如下所示

JSONObject parentArray = new JSONObject(finaljson);

JSONArray obj = parentArray.getJSONArray("data");
for (int i =0 ; i< obj.length(); i++) {
  JSONObject dataObj = obj.getJSONObject(i);
  String league_id = data.getString("league_id");
//With proper Null checks
  String teamName = data.getJSONObject("localTeam").getJSONObject("data").getString("name");
}

【讨论】:

  • 感谢您的回复。我想遍历对象并获取元素。如果您能提供代码帮助,我将不胜感激。以及如何获取本地团队的名称,因为它是数据的内部元素,是子数组 localteam。
  • 本地团队根据 json 是“对象”而不是数组。请找到更新的答案。每个对象都必须一个一个地迭代才能得到内部对象
  • 非常感谢。我实现它,看看。
猜你喜欢
  • 1970-01-01
  • 2020-02-06
  • 1970-01-01
  • 2014-03-10
  • 2015-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多