解析 Java Android 中的 JSON 对象时出错

Error while parsing JSON Object in Java Android

在我的 Android 应用程序中,我正在获取锦标赛积分 table 的详细信息,以将输出转换为从 JSON 对象到 Java

JSON对象如下图:

{
  "group": {
    "Teams": [
      {
        "name": "Team 1",
        "p": "10",
        "w": "9",
        "l": "1",
        "points": "18"
      },
      {
        "name": "Team 2",
        "p": "10",
        "w": "9",
        "l": "1",
        "points": "18"
      },
      {
        "name": "Team 3",
        "p": "10",
        "w": "9",
        "l": "1",
        "points": "18"
      },
      {
        "name": "Team 4",
        "p": "10",
        "w": "6",
        "l": "4",
        "points": "12"
      },
      {
        "name": "Team 5",
        "p": "10",
        "w": "6",
        "l": "4",
        "points": "12"
      },
      {
        "name": "Team 6",
        "p": "10",
        "w": "6",
        "l": "4",
        "points": "12"
      },
      {
        "name": "Team 7",
        "p": "10",
        "w": "5",
        "l": "5",
        "points": "11"
      },
      {
        "name": "Team 8",
        "p": "10",
        "w": "5",
        "l": "5",
        "points": "11"
      }
    ]
  }
}

Android Java 代码如下:

JSONObject match = new JSONObject(response);

if (match.has("group")) {
    JSONObject group = match.getJSONObject("group");

    if (match.has("Teams")) {
        JSONObject teams = group.getJSONObject("Teams");

        if (teams.has("0")) {
            JSONObject teams_object = teams.getJSONObject("0");
            String team_name = teams_object.getString("name");
            String matches_played = teams_object.getString("p");
            String matches_won = teams_object.getString("w");
            String matches_lost = teams_object.getString("l");
            String points = teams_object.getString("points");
        }
    }
}

但是我在通过 getMessage() 方法打印错误消息时遇到错误。下面是错误:

Error: Value ["name","p","w","l","points"] at header of type org.json.JSONArray cannot be converted to JSONObject

任何人都可以帮忙,比如我哪里出了问题或解决方法是什么?提前致谢

试试这个@SaAsh,我刚刚根据你的回答进行了编辑

if (match.has("group")) {
JSONObject group = match.getJSONObject("group");

if (match.has("Teams")) {
    JSONObject teams = group.getJsonArray("Teams");


        for(int i = 0 ; i < teams.length();i++){
        JSONObject teams_object = teams.getJSONObject("i");
        String team_name = teams_object.getString("name");
        String matches_played = teams_object.getString("p");
        String matches_won = teams_object.getString("w");
        String matches_lost = teams_object.getString("l");
        String points = teams_object.getString("points");
      }
    }
}
}

在您的 Json 团队中持有对象数组,您解析错误。 试试这个

 JSONObject jsonObject = new JSONObject(response);
 JSONObject groups = jsonObject.getJSONObject("group");
 JSONArray teams = groups.getJSONArray("Teams");
 for(int i=0;i<teams.length();i++){
        JSONObject obj = teams.getJSONObject(i);
        name.append(obj.getString("name")+"\n");
 }
try {
    JSONObject jsonObject = new JSONObject(response.body().string());
    JSONObject subObject = jsonObject.getJSONObject("group") ;
    JSONArray jsonArray = subObject.getJSONArray("Teams");
    for (int i=0; i<jsonArray.length(); i++){
        JSONObject object = jsonArray.getJSONObject(i);
        String name = jsonObject.getString("name");
        String p= jsonObject.getString("p");
        String w= jsonObject.getString("w");
        String l= jsonObject.getString("l");
        String points = jsonObject.getString("points");
    }
} catch (Exception e) {
     e.printStackTrace();
}
if (match.has("group")) {
    JSONObject group = match.getJSONObject("group");

    if (match.has("Teams")) {
        JSONArray teams = group.getJSONArray("Teams");

     // for only 0th element use below code else looping

                JSONObject teams_object = (JSONObject) teams.get(0);
                String team_name = teams_object.getString("name");
                String matches_played = teams_object.getString("p");
                String matches_won = teams_object.getString("w");
                String matches_lost = teams_object.getString("l");
                String points = teams_object.getString("points");

    }
}

Error: Value ["name","p","w","l","points"] at header of type org.json.JSONArray cannot be converted to JSONObject

您收到错误消息是因为有一组团队,并且您正在尝试使用 JSONObject

进行解析

请检查下面的代码片段。它会起作用。

try {
        JSONObject match = new JSONObject("your_response");
        if (match.has("group")) {
            JSONObject group = match.getJSONObject("group");

            if (group.has("Teams")) {
                JSONArray teams = group.getJSONArray("Teams");

                for(int i=0; i < teams.length(); i++){
                    JSONObject teams_object = (JSONObject) teams.get(i);
                    String team_name = teams_object.getString("name");
                    String matches_played = teams_object.getString("p");
                    String matches_won = teams_object.getString("w");
                    String matches_lost = teams_object.getString("l");
                    String points = teams_object.getString("points");
                }
            }
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

你的代码有 4 个错误,

1.You 必须检查 group 是否有 teams 而不是 matchteams.

2.You 必须将 team 作为 JSONArray 而不是 JSONObject,因为它是数组。

3.You 必须检查团队的大小,而不是查找具有键 0 的对象,您的组中没有名称为 0 的对象,

4.You 必须根据索引而不是键获取对象(即 0

请检查工作代码,这是经过测试的

try {
    JSONObject  match = new JSONObject(response);
    if (match.has("group")) {
        JSONObject group = match.getJSONObject("group");

        if (group.has("Teams")) {
            JSONArray teams = group.getJSONArray("Teams");

            if (teams.length() > 0) {
                JSONObject teams_object =(JSONObject) teams.get(0);
                String team_name = teams_object.getString("name");
                String matches_played = teams_object.getString("p");
                String matches_won = teams_object.getString("w");
                String matches_lost = teams_object.getString("l");
                String points = teams_object.getString("points");
                Log.v(TAG, team_name);
            }
        }
    }
} catch (JSONException e) {
    e.printStackTrace();
}