How to do JSON Parsing in Android?

JSON stands for JavaScript Object Notation.It is an independent data exchange format and is the best alternative for XML. All the responses from server are json encoded (if encoded i n PHP file). In order to parse them, following are the steps of JSON Parsing are:
1. For parsing a JSON object, we will create an object of class JSONObject and specify a string
containing JSON data to it.
2. An JSON file consist of different object with different key/value pair e.t.c. So JSONObject has a
seperate function for parsing each of the component of JSON file.

See the example below. It has json encoded string as:

  {
 "students": [
 {
 "name": "abc",
 "class": "xyz",
 "contact": {
 "email": "xyz@live.com",
 "phone": "1234567"
 }
 },
 {
 "name": "other student",
 "class": "other class",
 "contact": {
 "email": "abc@gmail.com",
 "phone": "456789"
 }
 }
 ]
}

You can check JSON valid string here : http://jsonlint.com

To parse this string, following is the code.

package com.example.jsonparse;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONException;

import android.util.Log;

public class MainActivity extends Activity {
 
 /*
 * 
 * (non-Javadoc)
 * @see android.app.Activity#onCreate(android.os.Bundle)
 * 
 * 
 * {
 * "students":[
 * {
 * "name":"abc",
 * "class":"xyz",
 * "contact":{
 * "email":"xyz@live.com",
 * "phone":"1234567"
 * }
 * },
 * {
 * "name":"other student",
 * "class":"other class",
 * "contact":{
 * "email":"abc@gmail.com",
 * "phone":"456789"
 * }
 * }
 * ]
 * }
 * 
 * 
 */
 
 String jsonData = "{\"students\":[{ \"name\":\"abc\",\"class\":\"xyz\",\"contact\":{\"email\":\"xyz@live.com\",\"phone\":\"1234567\"}},{\"name\":\"other student\",\"class\":\"other class\",\"contact\":{\"email\":\"abc@gmail.com\",\"phone\":\"456789\"}}]}";

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main_layout);

 
 }


 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
 
 // Inflate the menu; this adds items to the action bar if it is present.
 getMenuInflater().inflate(R.menu.main, menu);
 return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
 // Handle action bar item clicks here. The action bar will
 // automatically handle clicks on the Home/Up button, so long
 // as you specify a parent activity in AndroidManifest.xml.
 int id = item.getItemId();
 if (id == R.id.action_settings) {
 return true;
 }
 return super.onOptionsItemSelected(item);
 }
 
 public void onParseClick(View view)
 {
 JSONObject json = null;
 String TAG = "jsonParse";
 try {
 json = new JSONObject(jsonData);
 JSONArray myArray = json.getJSONArray("students");
 for (int i = 0 ; i < myArray.length() ; i++)
 {
 JSONObject myObj = myArray.getJSONObject(i);
 String name = myObj.getString("name");
 Log.d(TAG, "Name: "+name);
 String myclass = myObj.getString("class");
 Log.d(TAG, "Class: "+myclass);
 JSONObject contactDetails = myObj.getJSONObject("contact");
 String email = contactDetails.getString("email");
 Log.d(TAG, "Email: "+email);
 int phone = contactDetails.getInt("phone");
 Log.d(TAG,"Phone: "+phone);
 }
 }
 catch(JSONException e)
 {
 e.printStackTrace();
 return;
 }
 
 
 }
}

The output in log is:

jsonThats it. Continue to parse the json encoded response from the webserver and have fun šŸ™‚

Advertisements

About Priyanka Kapoor

Simple, Hardworking & friendly.....
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s