当前位置: 首页 > 知识库问答 >
问题:

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

孙熠彤
2023-03-14

我尝试了不同的方法在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/

共有1个答案

华福
2023-03-14

这应该是您的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();
        }
    }
 类似资料:
  • 我有以下格式的json文件: 那么,我如何在pig中解析这个json。。 此外,categories和rep中可以有一些char。。可能并不总是空的。我做了以下尝试。 但我得到这个错误: 组织。科德豪斯。杰克逊。JsonParseException:意外字符('D'(代码68)):在[源代码:java.io]处应为有效值(数字、字符串、数组、对象、“true”、“false”或“null”)。By

  • 问题内容: 我有一个,尝试检索并解析它,但出现此错误: 这是我的功能: 怎么了?我花了两天时间来解决这个问题。有时我会遇到空值,但我不知道如何使用它们。 问题答案: 添加与null测试不同的简单方法。 那你还有问题吗?

  • 问题内容: 我是Java编程的新手,需要通过网络解析一个复杂的JSON对象。过去一天,我一直在阅读有关GSON的文档,但能够完全解析这种类型的结构并没有太大的运气: 我已经能够使它与该问题类似地工作,但是无法弄清楚如何使该附加数组级别起作用。 问题答案: 使用GSON的正确格式是我正在寻找的格式:

  • 我在解析从Excel中的API检索的XML文件时遇到问题。我可以成功地检索数据集(如下所示),但我找到的将每个字段格式化为其自身单元格的表格式的解决方案没有奏效,我认为这是因为XML的格式化方式。 每个XML看起来都类似于下面的内容。可能需要独立提取多个消息ID。(这是我遇到的另一个问题。“消息ID”中的空格引发了各种错误。) 下面是我尝试使用的解决方案:如何使用vba解析XML 下面是我尝试拉取

  • 我收到了一个包含字符串和元组元素组合的CSV文件,但找不到正确解析它的方法。我错过了什么明显的东西吗? csvfile 第一行是标题,第二行开始数据 产量: csv.reader解析每行不同,因为结构复杂,嵌入了花括号元素。 ...但是我希望每行有20个元素。

  • 我真的希望你能在这件事上帮我....我需要从我的xml文件中获取特定的数据,但我卡在了一个点上,我不知道如何继续... 我想从网络获得:网络名称;From代码:mcc和mnc代码;From设置:名称、id、类型、参数名称、值; 这就是我的xml文件的结构: 这就是我目前掌握的......我真的不能再继续下去了...在字符串content=CNode.GetLastChild().GetTextCo