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 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 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 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 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:

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:

Figure 2: Loaded Information from JSON url

Figure 2 list all the information from the downloaded file.

Conclusion:

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.

kloc member
Hi,
Nice tutorial. However I am getting NetworkonMainThreadException at this line
HttpResponse response = client.execute(httpGet);
I will be grateful to you for your help.
Thank you,
Sujani
[+1 month ago]
Answer it

Mr.Bool Editor
Please see this question in SO http://stackoverflow.com/questions/6773828/httpclient-execute-always-gives-exception[+1 month ago]
Answer it

Thejas Kumar S R
How to access value of operator name and experience points?
[+1 month ago]
Answer it

prachi
How to get name and experience points in this.
[+1 month ago]
Answer it

Dave Dave
There is a couple of things you will need to change to get this to work-
1. The variables VTYPE,VCOLOR,FUEL,TREAD declared in the Activity must match the json labels so instead of whats shown in the code use this:
private static final String VTYPE = "vehicleType";
private static final String VCOLOR = "vehicleColor";
private static final String FUEL = "fuel";
private static final String TREAD = "treadType";
Also you will need to create an android layout xml file called "list_item.xml" under "res/layout". The one I created looked like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/vehicleType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="vehicleType" />
<TextView
android:id="@+id/vehicleColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="vehicleColor" />
<TextView
android:id="@+id/fuel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fuel" />
<TextView
android:id="@+id/treadType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="treadType" />
</LinearLayout>
You will notice that the android:id of each TextView (and the xml file name itself) corresponds with the items in this line of code from the class ProgressTask-
ListAdapter adapter = new SimpleAdapter(context, jsonlist, R.layout.list_item, new String[]{VTYPE,VCOLOR,FUEL,TREAD}, int[]{R.id.vehicleType, R.id.vehicleColor,R.id.fuel,R.id.treadType});
Hope this helps.
[+1 month ago]
Answer it

Dave Dave
Sorry for the poor formatting in my post. It didn't look like that when I submitted it. Let me try a test here to see if I can fix it up-
There is a couple of things you will need to change to get this to work-
1. The variables VTYPE,VCOLOR,FUEL,TREAD declared in the Activity must match the json labels so instead of whats shown in the code use this:
private static final String VTYPE = "vehicleType";
private static final String VCOLOR = "vehicleColor";
private static final String FUEL = "fuel";
private static final String TREAD = "treadType";
2. You will need to create an android layout xml file called "list_item.xml" under "res/layout". The one I created looked like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="@+id/vehicleType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="vehicleType" />
<TextView android:id="@+id/vehicleColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="vehicleColor" />
<TextView android:id="@+id/fuel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fuel" />
<TextView android:id="@+id/treadType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="treadType" />
</LinearLayout>
You will notice that the android:id of each TextView (and the xml file name itself) corresponds with the items in this line of code from the class ProgressTask-
ListAdapter adapter = new SimpleAdapter(context, jsonlist,
R.layout.list_item,
new String[]{VTYPE,VCOLOR,FUEL,TREAD},
int[]{R.id.vehicleType, R.id.vehicleColor,R.id.fuel,R.id.treadType});
Hope this helps
[+1 month ago]
Answer it

Mr.Bool Editor
Thanks for your comment Dave.
You can use the pastebin.com to post the code and it will be shown correctly.[+1 month ago]
Answer it

virginia
Thanks Dave; that was really helpfull to make it work. Best regards![+1 month ago]
Answer it

Karl-Johan Schmidt
I have created the list_item.xml, and done everything else in the tutorial. But when i run my app, it just opens the Main_Activity with a white screen and thats it.
This list_item.xml, should it be connect to some java class? and if so, which one? [+1 month ago]
Answer it

Diogo Souza
Hi,
Please take a look at this line code:
setContentView(R.layout.activity_main);
It means that you should always reference the view that activity belongs to. In this case, just use R class, and will be enough.[+1 month ago]
Answer it

vivaswan
if we tap on the "excavator" item on the first screen then how to display "name" and "experiencepoints" on the next screen ??
[+1 month ago]
Answer it

Diogo Souza
Hi Vivas,
You just need to do the same as the previous activity. Just copy the same behavior as the previous one...[+1 month ago]
Answer it

tunahan
It gave an error at that part:
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 });
R.layout.list_item, R.id.vehicleType, R.id.vehicleColor, R.id.fuel, R.id.treadType . Because in xml file there is no ids like that. and if we add it and try to run it also gave an error. so what is the problem
[+1 month ago]
Answer it

tunahan
When I download ur source code it works well but when i write it it dont :D.
Actually even it works well i cannot understand the function of R.layout.list_item, R.id.vehicleType, R.id.vehicleColor, R.id.fuel, R.id.treadType ??[+1 month ago]
Answer it

Mr.Bool Editor
Hi Tunahan,
What exactly you don't understand on it? Those are just elements from the JSON document, used in the author's example.[+1 month ago]
Answer it

tunahan
Yeah they re elements from json document for ex. what R.id.list_item refers? As i know there should be one item at xml file with the "list_item" id. is that wrong?[+1 month ago]
Answer it

Mr.Bool Editor
Yes, actually if you pay attention author leaves space to put the list items in the xml doc.
You should create your list to receive the JSON data converted...[+1 month ago]
Answer it

tunahan
one more problem. when I try to open new activity with Intent it gave an error. Because simple adapter use "extends ListActivity" but Intent(this, Posts.class); needs a "extends Activity". how can open a new activity when I use a simple adapter
[+1 month ago]
Answer it

Mr.Bool Editor
Hi Tunahan,
If you take a look at the official ListActivity's Javadoc (http://developer.android.com/reference/android/app/ListActivity.html) you'll see that it inherits from Activity all the same methods we'd need.
The problem is maybe something else. Could you please post here the error stack?[+1 month ago]
Answer it

Mark Flarup
I'm stuck with a blank page at my phone when I run your code. I have no idea where I've gone wrong! Have anyone else struggled with the same issue? I have made the list_item.xml file as well and really don't know where I've skipped out something.
[+1 month ago]
Answer it

Mr.Bool Editor
Hello Mark, thanks for your comment, did you tried to download the source code by clicking in the source code link and tested the code ?[+1 month ago]
Answer it

Paulo
You can provide this complete project for download? This occurs many mistakes when I type, even typing correctly!
[+1 month ago]
Answer it

Mr.Bool Editor
Hi Paulo,
We do have the source code to download, you just need to click in button "Source Code" in the top of this article.[+1 month ago]
Answer it

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,00This 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,00This 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!