Google+

Thursday, June 30, 2011

JSON Data Parsing in Android - Part - 1

We are discussing the following points to Parse JSON data in android
  • Why JSON Format?
  • Syntax & Structure of JSON Format
  • Relation Between Android  & JSON
  • Steps to Parse JSON data in Android
  • Sample Application
 Why JSON [JavaScript Object Notation] Format?
  • Lightweight data-interchangeable format
  • Text format that is completely language independent and easy to read
  • JSON is
    - A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, Key-Value Pair , or associative array.
    - An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence. 


 Syntax & Structure of JSON Format
  • With Normal Object Collection
    Object_Name : {Key1 : Value1 , Key2 : Value2 , Key3 : Value3}

    Example =

    {
          "FileMenu": {
                                   "subMenu":"New","details":"Create New File",
                                   "subMenu":"Open","details":"Open Existing File",
                                   "subMenu":"Save","details":"Save File"
                             }
    }
  • With Array Based Object Collection
    Object_Name : [
                                   {Key1_1 : Value1_1 , Key1_2 : Value1_2 , Key1_3 : Value1_3},
                                   {Key2_1 : Value2_1 , Key2_2 : Value2_2 , Key2_3 : Value2_3}
                          ]

    Example =
    {
          "Menu": {
                             "menu":"File",
                             "value":"File",
                             "popup":{
                                                    "MenuItem":
                                                    [
                                                                 {"subMenu":"New","details":"Create New File"},                                                                            {"subMenu":"Open","details":"Open Existing File"},
                                                                 {"subMenu":"Save","details":"Save File"}
                                                     ]
                                          }
                        }
    }

Relation Between Android & JSON

Android already have inbuild JSON libraries. The Following class are used to parse the JSON Object.

    JSONArray          A JSONArray is an ordered sequence of values.
    JSONObject        A JSONObject is an unordered collection of name/value pairs.
    JSONStringer     JSONStringer provides a quick and convenient way of producing JSON text.
    JSONTokener     A JSONTokener takes a source string and extracts characters and tokens from it.




Steps to Parse JSON Data in Android






1. Create a JSON Object and Convert JSONString to JSON Object
    JSONObject jsonObject = new JSONObject(jsonDataString);
   
2. Create a Key based JSONObject.
    JSONObject menuObject = jsonObject.getJSONObject("Menu");

3. Get the Value
    Log.d("JSON", "Menu = " + menuObject.getString("menu"));
    Log.d("JSON",
"Value = " + menuObject.getString("value"));





Android JSON  Example




public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         String myJsonContent = "{\"Menu\": {\"menu\":\"File\",\"value\":\"File\",\"popup\":{\"MenuItem\":[{\"subMenu\":\"New\",\"details\":\"Create New File\"},{\"subMenu\":\"Open\",\"details\":\"Open Existing File\"},{\"subMenu\":\"Save\",\"details\":\"Save File\"}]}}}";
        sampleJsonParser(myJsonContent);
    }

    public void sampleJsonParser(String jsonString) {
        try {
            JSONObject jObject = new JSONObject(jsonString);
            JSONObject menuObject = jObject.getJSONObject("Menu");
            Log.d(
"JSON", "Menu = " +menuObject.getString("menu"));
            Log.d(
"JSON", "Value = " + menuObject.getString("value"));
            JSONObject popupObject = menuObject.getJSONObject("popup");
            JSONArray menuitemArray = popupObject.getJSONArray("MenuItem");
            for (int i = 0; i < 3; i++) {
                Log.d("JSON",
"Name = " + menuitemArray.getJSONObject(i).getString("subMenu")
                        .toString());
                Log.d("JSON",
"Value = " +menuitemArray.getJSONObject(i).getString(
                        "details").toString());
            }
       } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

Download Source Code & SVN Repository



Check Your Log with  Tag JSON

Enjoy The Code................




Don't Forget to Leave Comments....

2 comments:

andry said...

json is a very interesting language to be used. very good tutorial and can hopefully help me in building json in the application that I created for this lecture. thank you

Sachin Shelke said...

Your Most Welcome.

Google+