× Please, log in to give us a feedback. Click here to login

You must be logged to download. Click here to login


MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation


MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to use JSON to parse data into android application

In this article, we will learn what is JSON and why do we use JSON with android applications. Also we will see how do we use JSON parsing in android application.

JSON stands for JavaScript Object Notation is the most popular way to serialize and transmitting data over network. In android, application uses JSON to transmit data over networks and JSON data is parsed, while it is received from cloud servers and from some where else.

JSON Parsing with Android Application in Detail:

JSON provides data in form of object that is to be parsed while reading form source and a JSON object is created, whenever data is to be transmitted over network or any source to destination.

Listing 1: JSON Data Object

        "vehicleType": "excavator",
        "vehicleColor": "yellow",
        "fuel": "diesel",
                "name": "Greg Stark",
                "experiencePoints": 13
                "name": "Aarti Patel",
                "experiencePoints": 21
                "name": "Tarek Mizan",
                "experiencePoints": 9
        "treadType": "plate chain"
        "vehicleType": "Dump Truck",
        "vehicleColor": "yellow",
        "fuel": "gasoline",
                "name": "Marc Gervais",
                "experiencePoints": 8
                "name": "Atsuko Iwamoto",
                "experiencePoints": 3
                "name": "Chadwick Price",
                "experiencePoints": 54
        "treadType": "wheel"
        "vehicleType": "Grader",
        "vehicleColor": "green",
        "fuel": "diesel",
                "name": "Arun Verma",
                "experiencePoints": 34
                "name": "Morgan Parker",
                "experiencePoints": 56
                "name": "Paul Turner",
                "experiencePoints": 17
        "treadType": "wheel"

Listing 1 defines a JSON object that has three sub - objects with fives values. Fourth property of sub - object has one more sub - object with three sub - values. This document provides vehicle type and its information as JSON object that will be parsed and will be displayed into the android tabular format.

Let’s define MainActivity Java class file that will manage android application, when the application is started. It manages the first view of the application, while it’s loaded into the memory. It is listed into the Listing 2:

Listing 2: MainActivity.java

package com.test.jsonparser;

import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.jitesh.androidjsonparser.R;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends ListActivity {
	private Context context;
	private static String url = "http://docs.blackberry.com/sampledata.json";

	private static final String VTYPE = "Type";
	private static final String VCOLOR = "Color";
	private static final String FUEL = "Fuel";
	private static final String TREAD = "Tread";
	ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();
	ListView lv ;

	protected void onCreate(Bundle savedInstanceState) {
		new ProgressTask(MainActivity.this).execute();

	private class ProgressTask extends AsyncTask<String, Void, Boolean> {
		private ProgressDialog dialog;

		private ListActivity activity;

		// private List<Message> messages;
		public ProgressTask(ListActivity activity) {
			this.activity = activity;
			context = activity;
			dialog = new ProgressDialog(context);

		private Context context;

		protected void onPreExecute() {
			this.dialog.setMessage("Progress start");

		protected void onPostExecute(final Boolean success) {
			if (dialog.isShowing()) {
			ListAdapter adapter = new SimpleAdapter(context, jsonlist,
					R.layout.list_item, new String[] { VTYPE, VCOLOR,
							FUEL, TREAD }, new int[] {
							R.id.vehicleType, R.id.vehicleColor, R.id.fuel,
							R.id.treadType });


			// select single ListView item
			 lv = getListView();

		protected Boolean doInBackground(final String... args) {

			JSONParser jParser = new JSONParser();

			// get JSON data from URL
			JSONArray json = jParser.getJSONFromUrl(url);

			for (int i = 0; i < json.length(); i++) {

				try {
					JSONObject c = json.getJSONObject(i);
					String vtype = c.getString(VTYPE);

					String vcolor = c.getString(VCOLOR);
					String vfuel = c.getString(FUEL);
					String vtread = c.getString(TREAD);

					HashMap<String, String> map = new HashMap<String, String>();

					// Add child node to HashMap key & value
					map.put(VTYPE, vtype);
					map.put(VCOLOR, vcolor);
					map.put(FUEL, vfuel);
					map.put(TREAD, vtread);
				catch (JSONException e) {
			return null;

Listing 2 defines MainActivity.java class that extends ListActivity class and reads a json file form the url placed “http://docs.blackberry.com/sampledata.json”. It also define four other class variables “VTYPE”, “VCOLOR”, “FUEL” and “TREAD” with its default values. It also define a ArrayList collection type variable that is used to list all the parsed data from the url and list it into the Android Screen, while the application is loaded. Once the data is read from the url, it is parsed into JSONArray using the JSONParser class that is converted into string and added to HashMap colleciton type object and displayed into the ListView format of the MainActivity screen of the application.

Lets define next java class that is used to parse JSON data, listed into Listing 3:

Listing 3: JSONParser.java

package com.test.jsonparser;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;

import android.util.Log;

public class JSONParser {

	static InputStream iStream = null;
	static JSONArray jarray = null;
	static String json = "";

	public JSONParser() {

	public JSONArray getJSONFromUrl(String url) {

		   StringBuilder builder = new StringBuilder();
		    HttpClient client = new DefaultHttpClient();
		    HttpGet httpGet = new HttpGet(url);
		    try {
		      HttpResponse response = client.execute(httpGet);
		      StatusLine statusLine = response.getStatusLine();
		      int statusCode = statusLine.getStatusCode();
		      if (statusCode == 200) {
		        HttpEntity entity = response.getEntity();
		        InputStream content = entity.getContent();
		        BufferedReader reader = new BufferedReader(new InputStreamReader(content));
		        String line;
		        while ((line = reader.readLine()) != null) {
		      } else {
		        Log.e("==>", "Failed to download file");
		    } catch (ClientProtocolException e) {
		    } catch (IOException e) {
		// Parse String to JSON object
		try {
			jarray = new JSONArray( builder.toString());
		} catch (JSONException e) {
			Log.e("JSON Parser", "Error parsing data " + e.toString());

		// return JSON Object
		return jarray;


Listing 3 define next class java class JSONParser.java that defines a method getJSONFromUrl() that gets data from given url and parse it into JSONArray format. JSONArray is parsed into string and is added into the hashmap collection type object that is parsed into the list view of the android application and is displayed in main activity of the android application.

Listing 4: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    android:versionName="1.0" >

        android:targetSdkVersion="16" />
    <uses-permission android:name="android.permission.INTERNET"/>

        android:theme="@style/AppTheme" >
            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />


Listing 4 defines AndroidManifest.xml file that defines application configuration settings and the android api version that is supported by this application.

Listing 5: string.xml

<?xml version="1.0" encoding="utf-8"?>

    <string name="app_name">Vehicle Information</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>


Listing 5 defines the application captions that will be used, while application starts, like application name and etc.

Listing 6: activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	<!-- Main ListView 
		 Always give id value as list(@android:id/list)


Listing 6 defines the main activity layout of the application that list the list items pattern that is to be displayed.

While this application is ready to run on android device and it is started loading json file from the given url as shown into the figure 1:

Main Activity Loading JSON Information

Figure 1: Main Activity Loading JSON Information

Figure 1 shows the process started , while loading the json file information from the given url. Once file is loaded from the url, it is parsed and the information is displayed into the screen as shown ni figure 2:

Loaded Information from JSON url

Figure 2: Loaded Information from JSON url

Figure 2 list all the information from the downloaded file.


In this article, we learn about the json and also came to know about the use of using json with android application. We also went through with an example application in this article.

Have more than 5 years experience about Java, .Net, C and C++.

What did you think of this post?
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!

> More info about MrBool Credits
You must be logged to download.

Click here to login