0

In my Android app, I am fetching the details of Points table of a tournament to convert the output to a string from JSON object to Java

JSON object is shown below:

{
  "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 Code is below:

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

But I am getting the error where I print the error message through getMessage() method. Here is the error below:

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

Can anyone please help like where I am going wrong or what is the fix ? Thanks in advance

SaAsh Techs
  • 169
  • 10

6 Answers6

1

In your Json Teams holds the Array of Object and you are parsing wrong. Try this

 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");
 }
Rajnish suryavanshi
  • 3,168
  • 2
  • 17
  • 23
0

Try this @SaAsh, I have just edited by taking your answer

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");
      }
    }
}
}
Brahma Datta
  • 1,102
  • 1
  • 12
  • 20
0
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();
}
Kanwarpreet Singh
  • 2,037
  • 1
  • 13
  • 28
0
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");

    }
}
Basi
  • 3,009
  • 23
  • 28
0

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

You are getting the error because there is an Array of Teams and you are trying parse with JSONObject

Please check below code snippet. It will work.

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();
    }
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
0

There is 4 mistakes in your code,

1.You have to check whether group has teams instead match has teams.

2.You have to get team as JSONArray instead of JSONObject because it is array.

3.You have to check the size of team, instead of find the object with key 0, there is no object with name 0 in your group,

4.You have to get the object based on index instead of key(ie., 0)

Please check the working code, this is tested

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();
}
Gunaseelan
  • 14,415
  • 11
  • 80
  • 128