Parsing JSON Object in ANDROID.

By | June 9, 2011

JSON are alternative to XML and easy to understand than XML.
As other languages Java also supports JSON parsing.
Here is a simple example of JSON parsing in ANDROID.

This is the JSON String that I am going to parse.

           { "A1":"A1_value" ,"A2":"A2_value","sub":
              { "sub1":[ {"sub1_attr":"sub1_attr_value" }]}

And this JSON is same as this xml.

	  <A A1="A1_value" A2="A2_value">
	 		<sub1 sub1_attr="sub1_attr_value" />

Here is the code for this parsing.

package pack.coderzheaven;

import org.json.JSONArray;
import org.json.JSONObject;
import android.os.Bundle;

public class JSON_Demo extends Activity {
	private JSONObject jObject;
	private String test = "{"A":  {  "A1":"A1_value" ,"A2":"A2_value", "sub": { "sub1":[ {"sub1_attr":"sub1_attr_value" }] } }    }";

	public void onCreate(Bundle savedInstanceState) {
		try {
		} catch (Exception e) {
	private void parse() throws Exception {

		jObject = new JSONObject(test);
		JSONObject menuObject = jObject.getJSONObject("A");
		String attributeId1 = menuObject.getString("A1");
		System.out.println("A1 value == " +attributeId1);
		String attributeId2 = menuObject.getString("A2");
		System.out.println("A2 value == " +attributeId2);

		JSONObject popupObject = menuObject.getJSONObject("sub");
		JSONArray menuitemArray = popupObject.getJSONArray("sub1");

		System.out.println("sub1_attr = " + menuitemArray.getJSONObject(0).getString("sub1_attr").toString());

Please check the Logcat for the output.

Posts to come.

More complex JSON String parsing in ANDROID.

Please leave your valuable comments.

5 thoughts on “Parsing JSON Object in ANDROID.

  1. Pingback: Parsing JSON Objects in ANDROID. | LetAndroid | Android News

  2. Pingback: Complex JSON String parsing in ANDROID | Coderz Heaven

  3. Pingback: Mr. Android » Blog Archive » Parsing JSON Objects in ANDROID.

  4. Pingback: JSON IN ANDROID. | CoderzHeaven

  5. Pingback: How to read webpage contents as a string in Android?

Leave a Reply

Your email address will not be published.