Android JSON Parsing tutorial

By -

Android JSON Parsing Example

I have been noticing many novice android programmers are facing issues in implementing JSON Parsing, so here is my try to write an article with step by step demo.

Many times we get JSON string as a response from web server, now we have to parse the JSON string into either array, objects or values to display the response in the particular views like ListView, TextView, etc.

We can use JSONObject and JSONArray for parsing the response string.

Now, consider this sample JSON response string for the understanding of parsing example:

{"FirstObject": { "attr1":"one value" ,"attr2":"two value",

"sub": { "sub1":[ {"sub1_attr":"sub1_attr_value" },{"sub1_attr":"sub2_attr_value" }]}
}
}

And to validate JSON String, i would suggest you JSONLint.com

android json parsing
Now, let me start to give step by step demo for parsing the same JSON resoponse:

Step – 1:
create a JSONObject with the received response string:

JSONObject jsonObject = new JSONObject(strJSONResponse);

Step – 2:

Get the main object from the created json object by using getJSONObject() method:

JSONObject object = jsonObject.getJSONObject("FirstObject");

Step – 3:
Now this FirstObject contains 2 strings namely “attr1″,”attr2″ and a object namely “sub”.
So get 2 strings by using getString() method.

String attr1 = object.getString("attr1");
String attr2 = object.getString("attr2");

and get a sub object by using the same getJSONObject() method as we have used above:

JSONObject subObject = object.getJSONObject("sub");

Step – 4:

Now this “sub” sub-object contains 1 array namely “sub1″. So we can get this JSON array by using
getJSONArray() method:

JSONArray subArray = subObject.getJSONArray("sub1");

Now, we just need to process this array same as simple string array:

for(int i=0; i<subArray.length(); i++)
{
strParsedValue+="\n"+subArray.getJSONObject(i).getString("sub1_attr").toString();
}

You are done with JSON Parsing. Enjoy :)

Output Snapshot:

android json parsing

Full source code:

package com.technotalkative.jsonparsing;

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

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class JSONParsingActivity extends Activity {
	/** Called when the activity is first created. */

	TextView txtViewParsedValue;
	private JSONObject jsonObject;

	String strParsedValue = null;

	private String strJSONValue = "{\"FirstObject\":{\"attr1\":\"one value\" ,\"attr2\":\"two value\","
			+ "\"sub\": { \"sub1\":[ {\"sub1_attr\":\"sub1_attr_value\" },{\"sub1_attr\":\"sub2_attr_value\" }]}}}";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		txtViewParsedValue = (TextView) findViewById(R.id.textView1);

		try {
			parseJSON();

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void parseJSON() throws JSONException {
		jsonObject = new JSONObject(strJSONValue);

		JSONObject object = jsonObject.getJSONObject("FirstObject");
		String attr1 = object.getString("attr1");
		String attr2 = object.getString("attr2");

		strParsedValue = "Attribute 1 value => " + attr1;
		strParsedValue += "\n Attribute 2 value => " + attr2;

		JSONObject subObject = object.getJSONObject("sub");
		JSONArray subArray = subObject.getJSONArray("sub1");

		strParsedValue += "\n Array Length => " + subArray.length();

		for (int i = 0; i < subArray.length(); i++) {
			strParsedValue += "\n"
					+ subArray.getJSONObject(i).getString("sub1_attr")
							.toString();
		}

		txtViewParsedValue.setText(strParsedValue);
	}
}

// Actual JSON Value
/*
 * { "FirstObject": { "attr1": "one value", "attr2": "two value", "sub": {
 * "sub1": [ { "sub1_attr": "sub1_attr_value" }, { "sub1_attr":
 * "sub2_attr_value" } ] } } }
 */

// Same JSON value in XML
/*
 * <FirstObject obj1="Object 1 value" obj2="Object 2 value"> <sub> <sub1
 * sub1_attr="sub1_attr_value" /> <sub1 sub1_attr="sub2_attr_value" /> </sub>
 * </FirstObject>
 */

Download this example: https://github.com/PareshMayani/Android-JSON-Parsing

Update: Read my latest article on JSON Parsing library to know and explore JSON Parsing libraries available which would be a faster way to implement JSON Parsing.
Paresh Mayani

Paresh Mayani is a lead android developer from India. Currently, He is Sr. Software engineer at InfoStretch Solutions Pvt. Ltd. He has been developing android apps since very 1st version i.e. Android 1.5, he says he has seen days and nights of Android history. He is the Head/Organizer of Google Developers Group (GDG), Ahmedabad

Loading Facebook Comments ...
Loading Disqus Comments ...