Tuesday 4 February 2014

JSON Parsing in Android

JSON Stands for JAVA Script Object Notation. It is light weight structured, easy to parse and very easy to human readable. JSON is independent data exchange formate and best alternate for  XML.

In this tutorial we are going to learn how to put values in JSON in Android, and how to parse a JSON data set in Android.

Android provides four differnet classes to manipulate JSON data. These classes are JSONArray,JSONObject,JSONStringer and JSONTokenizer.

Difference between [ and { (Square and Curly brackets).

Generally all JSON data set node starts with a square or curly brackets
when a json starts with square brackets its an Array and when a node starts with a curly bracket its an object. to manipulate an JSON array use JSONArray obj. and manipulating JSON object use JSONObject object.
in short
If your JSON node starts with [, then we should use getJSONArray() method. Same as if the node starts with {, then we should use getJSONObject() method.

package com.ashfaq.jsonparsing;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class XMLDemo extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
               
        putValueJson();  // values putting in json
        jsonParsing();   // Json parsing        
       
    }


    private void putValueJson() {
        // TODO Auto-generated method stub
        JSONObject obj = new JSONObject();
        try {
            obj.put("name", "Ashfaq");
            obj.put("PG", true);
            obj.put("num", new Integer(100));
            obj.put("balance", new Double(1000.15));
           
            Log.d("Json : ",""+ obj);
           
           
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
   
    private void jsonParsing() {
       
        String s = "{\"FirstObject\":{\"attr1\":\"one value\" ,\"attr2\":\"two value\","
                +"\"sub\": { \"sub1\":[ {\"sub1_attr\":\"sub1_attr_value\" },{\"sub1_attr\":\"sub2_attr_value\" }]}}}";
       
        try {
            JSONObject jobj = new JSONObject(s);
            JSONObject nobj = jobj.getJSONObject("FirstObject");
            String atr1 = nobj.getString("attr1");
            String atr2 = nobj.getString("attr2");
           
            Log.d("json value  atr1 ", atr1 +" atr2 "+atr2);
           
            JSONObject sobj = nobj.getJSONObject("sub");
            JSONArray bary = sobj.getJSONArray("sub1");
            String s2 = "" ;
            for(int i=0;i<bary.length();i++){
            //     s2 = "\n"+bary.getJSONObject(i).getString("sub1_attr").toString();
               
                 JSONObject jsno = bary.getJSONObject(i);
                 String astr = jsno.getString("sub1_attr");
                // String astr1 = jsno.getString("sub1_attr");
                    Log.d("json value ",astr );
            }
           
   
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
    }

}