0
[
  {
    "countries": [
      {
        "id": 1,
        "country": "India"
      },
      {
        "id": 2,
        "country": "Australia"
      },
      {
        "id": 3,
        "country": "Srilanka"
      },
      {
        "id": 4,
        "country": "Pakistan"
      },
      {
        "id": 5,
        "country": "Switzerland"
      }
    ]
  }
]

How to parse this Json Response in Android. i am not getting any proper solution. please help me.

2
  • See this tutorial. It helps you. Commented Aug 13, 2012 at 4:24
  • You need to have any name for the array.You have not mentioned it. Commented Aug 13, 2012 at 4:29

4 Answers 4

4

Do it like this...

JSONArray arr = locs.getJSONArray("countries");


for (int i = 0; i < arr.length(); ++i) {
    JSONObject rec = arr.getJSONObject(i);
    int id = rec.getInt("id");
    String con = rec.getString("country");
    // ...
}
Sign up to request clarification or add additional context in comments.

Comments

1
   JSONArray jArr=new JSONArray(response);
    for(int i=0;i<jArr.length;i++)
    {
    id[]=jArr.getJSONObject(i).getInt("id");
    con[]=jArr.getJSONObject(i).getString("country");
    }

Comments

0

http://developer.android.com/reference/org/json/JSONTokener.html

I've also seen Jackson used in Android apps.

Comments

0
 JSONArray countriesobj = new JSONArray();   
 JSONObject json;
            try {
                JSONObject jObject = new JSONObject(response);
                json = jObject;

                countriesobj = json.getJSONArray("countries");

                country = new String[countriesobj.length()];
                for (int i = 0; i < countriesobj.length(); i++) {

                    JSONObject e = countriesobj.getJSONObject(i);

                    country[i] = e.getString("country");

                }

            } catch (Exception e) {
                e.printStackTrace();

            }

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.