Android Json Parsing Tutorial

Let's start to how to parse Json Objects in Android.
First of all when you start parse Json Objects Check your Json String is valid or not.
Here You can Check Your Json String


here is my Json String.
{
    "student": [
        {
            "id": 1,
            "name": "Anil",
            "city": "Surat",
            "Gender": "M",
            "age": 10,
            "birthdate": "23/05/2002"
        },
        {
            "id": 2,
            "name": "Kapil",
            "city": "Baroda",
            "Gender": "M",
            "age": 12,
            "birthdate": "08/01/2000"
        },
        {
            "id": 3,
            "name": "Priya",
            "city": "Ahmedabad",
            "Gender": "F",
            "age": 14,
            "birthdate": "01/03/1998"
        }
    ]
}

Now above Json String have as Student array.that So first,check your Json String is Starting with `{` then its start with JSONObject.and if its start with `[` then its start with JSONArray.

1.JSONObject 
so here we have JSONObject, 
JSONObject mainJson = new JSONObject(jsonString);
Now we have array  student so we can get JSONArray from JSONObject.and in getJSONArray method have String paramter now add your JSONArray name.
JSONArray jsonArray = mainJson.getJSONArray("student");
Getting JSONArray now we have  JSONObject of as in JSONArray.So using for loop we can get one by one  JSONObject.
for (int i = 0; i < jsonArray.length(); i++) {
     JSONObject objJson = jsonArray.getJSONObject(i);
                // here you can get id,name,city...
      int id = objJson.getInt("id");
     String name =objJson.getString("name"); 
               String gender=objJson.getString("Gender");   
}

2.JSONArray 
 if your Json String Start with JSONArray then you just,
JSONArray jsonArray = new JSONArray(jsonString); 
now as per above jsonArray ,retrieve value.

3.How to check JSONString have Array or any Fileld And those are null or not?
 Using has(String) you can check  JSONObject have array or filed
if(objJson.has("student")){ }
if(objJson.has("id")){ } 
There are one more thing is check null for jsonObject.
if(objJson.isNull("name")){} 
4.How to Remove JSONObject from JSONArray?
 There are no any method for removing jsonobject from JSONArray.So we have option List<T>.so List have remove method and you can remove your JSONObject.

5.How many Data type we can Retrieve from JSONObject?
Using JSONObject you can retrieve Strings, Booleans, Integers, Longs, Doubles or NULL. 

here is screen shot of above JsonString.

















DownLoad Full Source Code From Here. 
            DownLoad Source Code 


6 comments:

  1. I follow this example and its working I want to put a Toggle button or checkbox for Multi_Choice_Mode in this listview,
    Please help me......

    ReplyDelete
  2. i am getting this error while running your code
    org.json.JSONException: Value student at 0 of type java.lang.String cannot be converted to JSONObject

    ReplyDelete
  3. Good post... and I also learn from your code AsyncTask, Show progress dialog and many more things. Thanks you....

    ReplyDelete
  4. f9 i m very happy.before i tried so many codes bt not work .yr code works great job

    ReplyDelete
  5. Nice one !

    Helped me a lot :D

    ReplyDelete

Android Testing App