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

17
  • pojo.sodhanalibrary.com/Convert Commented May 9, 2019 at 5:39
  • use POJO classes. Parsing will be much more easy Commented May 9, 2019 at 5:40
  • check my answer @SaAsh Commented May 9, 2019 at 5:43
  • Checking @g.brahmaDatta Commented May 9, 2019 at 5:47
  • Team should be Json array. not a Json Object. Commented May 9, 2019 at 5:48

6 Answers 6

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");
 }
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, I am getting the error 'Cannot resolve method getJSONArray(java.lang.String)' @Rajnish suryavanshi
what string name you are passing?
check my updated code. I checked and I am getting output as well.
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();
}

Comments

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

1 Comment

@SaAshTechs Share your logcat, then I will be able to help you out and full activity code.
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");

    }
}

1 Comment

@SaAshTechs better you go for gson parsing
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();
    }

1 Comment

@SaAshTechs Update your question with latest code, JSON and error
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");
      }
    }
}
}

1 Comment

Are you passing any header type while calling it to server? @SaAshTechs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.