Parse JSON Data In Android

JSON stands for JavaScript Object Notation. JSON is a light weight data interchange format in a text format so it can be easily parsed and read by machines and humans.

Parsing JSON data is important in Android. Create a new project in Android by using the following steps:

Step 1: 

 

Step 2:

Step 3:

 
Step 4:
 

Step 5:
 
 
JSON uses the following two types of different structures: 
  1. Collection of name/value pair
  2. Array

Example of collection of name/value pair: The { (curly brace) represents the JSON object.

  1. {"Employee" :{  
  2.   
  3.    "id":"1",  
  4.    "name":"David",  
  5.   
  6.    "address":"Delhi",  
  7.   
  8.    "zip":"110002"}  
  9.   
  10. }  

Example of collection of array: The [ (square bracket ) represents the JSON array.

  1. {  
  2.     "Employee": [  
  3.     {  
  4.         "id""1",  
  5.         "name""David",  
  6.         "address""Delhi",  
  7.         "zip""110002"  
  8.     },  
  9.     {  
  10.         "id""2",  
  11.         "name""John",  
  12.         "address""Mumbai",  
  13.         "zip""400001"  
  14.     },  
  15.     {  
  16.         "id""3",  
  17.         "name""Mathew",  
  18.         "address""Delhi",  
  19.         "zip""110003"  
  20.     },  
  21.     {  
  22.         "id""4",  
  23.         "name""Mary",  
  24.         "address""Delhi",  
  25.         "zip""110010"  
  26.     }]  
  27. }  

Complete Java class to parse JSON data.

  1. {  
  2.     "Employee": [  
  3.     {  
  4.         "id""1",  
  5.         "name""David",  
  6.         "address""Delhi",  
  7.         "zip""110002"  
  8.     },  
  9.     {  
  10.         "id""2",  
  11.         "name""John",  
  12.         "address""Mumbai",  
  13.         "zip""400001"  
  14.     },  
  15.     {  
  16.         "id""3",  
  17.         "name""Mathew",  
  18.         "address""Delhi",  
  19.         "zip""110003"  
  20.     },  
  21.     {  
  22.         "id""4",  
  23.         "name""Mary",  
  24.         "address""Delhi",  
  25.         "zip""110010"  
  26.     }]  
  27. }  
  28. package com.test.ppandey.jsonparse;  
  29. import android.support.v7.app.ActionBarActivity;  
  30. import android.os.Bundle;  
  31. import android.view.Menu;  
  32. import android.view.MenuItem;  
  33. import android.widget.TextView;  
  34. import org.json.JSONArray;  
  35. import org.json.JSONException;  
  36. import org.json.JSONObject;  
  37. public class MainActivity extends ActionBarActivity  
  38. {  
  39.     @Override  
  40.     protected void onCreate(Bundle savedInstanceState)  
  41.     {  
  42.         super.onCreate(savedInstanceState);  
  43.         setContentView(R.layout.activity_main);  
  44.         TextView output = (TextView) findViewById(R.id.textView1);  
  45.         String strJson = "{\"Employee\" :[ {" + " \"id\":\"1\",\n" + "\"name\":\"David\",\n" + "\"address\":\"Delhi\", \n" + "\"zip\":\"110002\"}, {\n" + "\"id\":\"2\",\n" + "\"name\":\"John\",\n" + "\"address\":\"Mumbai\",\n" + "\"zip\":\"400001\"}, {\n" + "\"id\":\"3\",\n" + "\"name\":\"Mathew\",\n" + "\"address\":\"Delhi\",\n" + "\"zip\":\"110003\"}, {\n" + "\"id\":\"4\",\n" + "\"name\":\"Mary\",\n" + "\"address\":\"Delhi\",\n" + "\"zip\":\"110010\"} \n" + "]}";  
  46.         String data = "";  
  47.         try  
  48.         {  
  49.             JSONObject jsonRootObject = new JSONObject(strJson);  
  50.             //Instance of JSONArray that contains JSONObjects  
  51.             JSONArray jsonArray = jsonRootObject.optJSONArray("Employee");  
  52.             //Iterate the jsonArray and print the information of the JSONObjects  
  53.             for (int i = 0; i < jsonArray.length(); i++)  
  54.             {  
  55.                 JSONObject jsonObject = jsonArray.getJSONObject(i);  
  56.                 int id = Integer.parseInt(jsonObject.optString("id").toString());  
  57.                 String name = jsonObject.optString("name").toString();  
  58.                 String address = jsonObject.optString("address").toString();  
  59.                 String zip = jsonObject.optString("zip").toString();  
  60.                 data += "Employee " + i + " : \n id= " + id + " \n Name= " + name + " \n Address= " + address + " \n zip=" + zip + "\n ";  
  61.             }  
  62.             output.setText(data);  
  63.         }  
  64.         catch (JSONException e)  
  65.         {  
  66.             e.printStackTrace();  
  67.         }  
  68.     }  
  69.     @Override  
  70.     public boolean onCreateOptionsMenu(Menu menu)  
  71.     {  
  72.         // Inflate the menu; this adds items to the action bar if it is present.  
  73.         getMenuInflater().inflate(R.menu.menu_main, menu);  
  74.         return true;  
  75.     }  
  76.     @Override  
  77.     public boolean onOptionsItemSelected(MenuItem item)  
  78.     {  
  79.         // Handle action bar item clicks here. The action bar will  
  80.         // automatically handle clicks on the Home/Up button, so long  
  81.         // as you specify a parent activity in AndroidManifest.xml.  
  82.         int id = item.getItemId();  
  83.         //noinspection SimplifiableIfStatement  
  84.         if (id == R.id.action_settings)  
  85.         {  
  86.             return true;  
  87.         }  
  88.         return super.onOptionsItemSelected(item);  
  89.     }  
  90. }  

In above code, we created string containing JSON data and object of class JSONObject.

  1. JSONObject jsonRootObject = new JSONObject(strJson);   

And pass string containing JSON data to it.

Second step is to parse the JSON.

  1. JSONArray jsonArray = jsonRootObject.optJSONArray("Employee");  
  2. int id = Integer.parseInt(jsonObject.optString("id").toString());  
  3. String name = jsonObject.optString("name").toString();  
  4. String address = jsonObject.optString("address").toString();  
  5. String zip = jsonObject.optString("zip").toString();  
Run the application and we get the JSON data as in the following.
 
  

Up Next
    Ebook Download
    View all
    Learn
    View all