Android JSON Parsing Tutorial

Posted: 29/03/2013 in Android

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
JSON View is a good add on on Chrome and Firefox to view a JSON link

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
Advertisements

Trả lời

Mời bạn điền thông tin vào ô dưới đây hoặc kích vào một biểu tượng để đăng nhập:

WordPress.com Logo

Bạn đang bình luận bằng tài khoản WordPress.com Đăng xuất / Thay đổi )

Twitter picture

Bạn đang bình luận bằng tài khoản Twitter Đăng xuất / Thay đổi )

Facebook photo

Bạn đang bình luận bằng tài khoản Facebook Đăng xuất / Thay đổi )

Google+ photo

Bạn đang bình luận bằng tài khoản Google+ Đăng xuất / Thay đổi )

Connecting to %s