Android JSON Parsing tutorial

By -

Android JSON Parsing Example

I have found many newbie android programmers are facing issues in implementing JSON Parsing, this article is for them with step by step demo.

Now a days, almost all the web APIs are REST based and it returns JSON response. We have to parse the JSON string either into array, objects or values, to display into 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, you can use JSONLint.com like tool.

android json parsing
Now, let’s have a look into the step by step demo for parsing the above 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 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.

CEO & Co-Founder at SolGuruz® | Organiser @ GDG Ahmedabad | Top 0.1% over StackOverflow | 15+ years experienced Tech Consultant | Helping startups with Custom Software Development

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