在安卓中解析复杂的 JSON 文件时出错



我已经尝试了不同的方法来解析Android中的JSON文件,但是在打印它们时遇到错误。

这是我的整个 JSON 文件:

{ 
  conferences: [
    {
      id: "7ab1c9c0-4ffa-4f27-bcb6-be54d6ca6127",
      name: "EASTERN CONFERENCE",
      alias: "EASTERN",
      divisions: [
             {
             id: "1fad71d8-5b9e-4159-921b-9b98d0573f51",
             name: "Atlantic",
             alias: "ATLANTIC",
             teams: [
                    {
                    id: "4417d3cb-0f24-11e2-8525-18a905767e44",
                    name: "Lightning",
                    market: "Tampa Bay",
                    reference: "14",
                    rank: {
                          division: 1,
                          conference: 1,
                          clinched: "conference"

                      }
             },
                      {
                    id: "4416ba1a-0f24-11e2-8525-18a905767e44",
                    name: "Bruins",
                    market: "Boston",
                    reference: "6",
                    rank: {
                          division: 2,
                          conference: 2,
                          clinched: "playoff_spot"
                          }
                },
               ]
            },
          {
           id: "d5ec45c4-a691-43ac-9e6d-92de92e763e7",
           name: "Metropolitan",
           alias: "METROPOLITAN",
           teams: [
                  {
                  id: "4417eede-0f24-11e2-8525-18a905767e44",
                  name: "Capitals",
                  market: "Washington",
                  reference: "15",
                  rank: {
                         division: 1,
                         conference: 3,
                         clinched: "division"
                         }
             },
       ]
       }
      ]
     },
     {
      id: "64901512-9ca9-4bea-aa80-16dbcbdae230",
      name: "WESTERN CONFERENCE",
      alias: "WESTERN",
      divisions: [
                 {
                 id: "5e868c4d-c6a3-4901-bc3c-3b7a4509e402",
                 name: "Central",
                 alias: "CENTRAL",
                 teams: [
                        {
                        id: "441643b7-0f24-11e2-8525-18a905767e44",
                        name: "Predators",
                        market: "Nashville",
                        reference: "18",
                        rank: {
                              division: 1,
                              conference: 1,
                              clinched: "presidents_trophy"
                              }
                         }
                        ]
                  },
          {
          id: "17101b65-e8b9-4cda-a963-eea874aed81f",
          name: "Pacific",
          alias: "PACIFIC",
          teams: [
                 {
                 id: "42376e1c-6da8-461e-9443-cfcf0a9fcc4d",
                 name: "Golden Knights",
                 market: "Vegas",
                 reference: "54",
                 rank: {
                       division: 1,
                       conference: 3,
                       clinched: "division"
                       }     
                  },
]
}
]
}
]
}

这是我为使其工作而实现的代码:

@Override
        protected Void doInBackground(Void... arg0) {
            HttpHandler sh = new HttpHandler();
            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(url);
            Log.e(TAG, "Response from url: " + jsonStr);
            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);
                    // Getting JSON Array node
                    JSONArray con = jsonObj.getJSONArray("conferences");
                    JSONArray division = con.getJSONArray("divisions");
                    JSONArray teams = jsonObj.getJSONArray("teams");
                    for (int i = 0; i < teams.length(); i++) {
                        JSONObject c = con.getJSONObject(i);
                        String alias = c.getString("alias");
                        JSONObject t = teams.getJSONObject(i);
                        String name = t.getString("name");
                        JSONObject rank = t.getJSONObject("rank");
                        String div = rank.getString("division");
                        // tmp hash map for single fixture
                        HashMap<String, String> fixture = new HashMap<>();
                        // adding each child node to HashMap key => value
//                        fixture.put("alias", alias);
                        fixture.put("name", name);
                        fixture.put("division", div);
                        // adding contact to contact list
                        fixtureList.add(fixture);
                    }

下面是 onPostExecute 方法:

@Override
        protected void onPostExecute(Void aVoid) {
            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();
            ListAdapter adapter = new SimpleAdapter(
                    MainActivity.this, fixtureList,
                    R.layout.list_ranking_item, new String[]{/**"alias"**/ "name", "div"},
                    new int[]{/**R.id.ranking_name**/ R.id.ranking_team,
                    R.id.ranking_rank});
            lv.setAdapter(adapter);
        }

我已经从这个网站获得了帮助来布局我的功能:https://www.androidhive.info/2012/01/android-json-parsing-tutorial/

这应该是 json 字符串的正确解析器

public void parseJson(String jsonString) {
        try {
            JSONObject jsonObject = new JSONObject(jsonString);
            JSONArray conferences = jsonObject.getJSONArray("conferences");
            for (int i = 0; i < conferences.length(); i++) {
                JSONObject conference = (JSONObject) conferences.get(i);
                String id = conference.getString("id");
                String name = conference.getString("name");
                String alias = conference.getString("Alias");
                JSONArray divisions = conference.getJSONArray("divisions");
                for (int j = 0; j < divisions.length(); j++) {
                    JSONObject division = divisions.getJSONObject(j);
                    String divisionId = division.getString("id");
                    String divisionName = division.getString("name");
                    String divisionAlias = division.getString("alias");
                    JSONArray teams = division.getJSONArray("teams");
                    for (int k = 0; k < teams.length(); k++) {
                        JSONObject team = teams.getJSONObject(k);
                        String teamId = team.getString("id");
                        String teamName = team.getString("name");
                        String teamMarket = team.getString("market");
                        String teamReference = team.getString("reference");
                        JSONObject rank = team.getJSONObject("rank");
                        String rankDivision = rank.getString("division");
                        String rankConference = rank.getString("conference");
                        String clinched = rank.getString("clinched");
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

最新更新