0

This is my json code: {"teams" : ["Real Madrid", "Bayern Munich"]}

Now I want to parse the values of the "teams" in android.

I have tried to parse it by this way, but it didn't work:

JSONObject jsonObject = new JSONObject(result);
JSONArray jsonArray = jsonObject.getJSONArray("teams");
String team1 = jsonArray.getJSONObject(0).toString();
String team2 = jsonArray.getJSONObject(1).toString();

Please tell me how to make it to work.

3
  • what is the problem you are facing? are you getting in error ? Commented Jan 11, 2014 at 9:22
  • whats queryArray? I dont see the variable jsonArray being used Commented Jan 11, 2014 at 9:23
  • no, I don't get error, but I don't get the value too. Commented Jan 11, 2014 at 9:24

3 Answers 3

5

Assuming you are using the package org.json, You should try

   JSONObject jsonObject = new JSONObject(result);
   JSONArray jsonArray = jsonObject.getJSONArray("teams");
   String team1 = jsonArray.getString(0);
   String team2 = jsonArray.getString(1);
Sign up to request clarification or add additional context in comments.

Comments

1
JSONObject jsonObject = new JSONObject(result);
JSONArray jsonArray = jsonObject.getJSONArray("teams");

String team1 = jsonArray.optString(0);
String team2 = jsonArray.optString(1);

Comments

0

I think the issue is with the structure of the JSON which you are getting. Because there is an array but there is no object.Objects are inside curly braces {}. So by specifying index location you cannot retrieve the value. You might need to update the JSON format.

Comments

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.