dcsimg

To XML and Back: Using JSON in Android

JavaScript Object Notation (JSON) is competing with XML as the data encoding format of choice.

Yesterday’s champion — XML

When it comes to data exchange, the default choice is more often than not XML.

Prior to XML’s arrival, data was typically exchanged via fixed length or delimited data formats. Common delimiters are commas, tabs, pipes, and new line characters. Critics of these formats argue that they are too rigid, so XML grew as a popular alternative.

XML is a text-based, self-describing format with each field wrapped in a “tag” in the same way elements of an HTML file are described with container tags — in fact, HTML is a specific subset of XML.

You can open an XML file in a text editor and make sense of it, though ordinarily an XML file is consumed by a specific application which is expecting the data it contains. For example, most email readers today consume an RSS feed, which is a relatively simple XML format containing information about stories or articles. RSS feeds are found all over the Internet. For example, here is a link for articles from Linux Magazine.

The strength of XML — its easy to read and parse format — is also its weakness. While storage is more plentiful today and networks are faster, there is little need to send all of those verbose tags over the wire.

The new kid in town and potential “heir apparent” to succeed XML as the data exchange format of choice is the JavaScript Object Notation, or JSON.

Introducing JavaScript Object Notation

JSON is based on a subset of the JavaScript language and like XML, is a format designed to organize data. It is less verbose than XML and has a very simple syntax. We discuss JSON here in this article, but you may also care to have a look at http://json.org.

JSON can store “objects” and ordered lists of objects. An “object” may be a string or a number, or even a list of objects — in other words, you can nest JSON objects within one another to an arbitrary depth. In our earlier articles covering WebOS programming we used JSON quite a bit to organize data. Thinking about it, WebOS itself is entirely dependent upon JSON. Increasingly JSON is used in dynamic, client-side programming as you can even store and exchange string representation of JavaScript functions, which are subsequently “eval’d” at run time.

Sample JSON

In this article we take a look at using JSON with a simple Android application. Before we build the application, let’s take a look at a sample of JSON.

I have recently been listening to a book by Richard Stearns, President of World Vision. In doing some research on the man, I came across an interview conducted by business guru .

I was thinking that this would be ideal to read on my mobile phone. This sort of data has ordinarily been made available via XML formatted RSS feeds. I converted this html into JSON for the purposes of this article. What we see below is a subset of the data — the sample application, which you can find from the Linux Magazine Mobile code hosting site, contains the full JSON file.

{
"firstname":"Richard",
"lastname":"Stearns",
"almamater":"Cornell University",
"occupation":"President, World Vision",
"interview":
	{
		"source" : "http://blog.guykawasaki.com/2007/05/ten_or_so_quest.html#ixzz0giEIX0zY",
		"questions":
			[
			{
			"Question": "How much money does World Vision raise every year?",
			"Answer": "Worldwide, World Vision raises about $2 billion annually; the U.S. office, which I head up, raises about half of the total."
			},
			{
			"Question": "Is this the 80/20 rule where twenty percent of the people send in eighty percent of the money or are donations more spread out?",
			"Answer": "World Vision's strength is that we are supported by hundreds of thousands of faithful people who give us about a dollar a day by sponsoring children. Our \"major donors\" account for less than five percent of our total income. Also, for a non-profit, we have quite a diversified portfolio of revenue. Just over forty percent is cash from private citizens; thirty percent is government grants in food and cash; and about thirty percent are products donated from corporation--what we call \"gifts-in-kind.\""
			}
			]
	}
}

Though it is a simple format, it is easy to introduce errors into a JSON object. As such you may want to test your JSON objects outside of your application during development. I was able to make use of to find a missing comma. This site is now in my bookmarks!

Some things to notice about this JSON data:

  • All data is text based, organized into key/value pairs. There is very little “overhead” in this data.
  • Each JSON “object” is delimited by curly braces {}.
  • Note that the value associated with the key “interviews” is a JSON object itself — a nested object.
  • The “questions” value is an array of JSON objects, each containing two key/value pairs.
  • Double quotes are escaped with a backslash, just as in Java and C programming environments.

Now that we’ve got a feel for what JSON looks like, let’s build an Android application using JSON.

Sample Application

The focus of this application is on parsing JSON data and manipulating it within Android. In order to work with JSON within an Android application, we must import the package org.json:

import org.json.*;

This package contains a few classes. We will use the classes JSONObject and JSONArray. For more info on this package, have a look at the Android docs online.

Our application has a very simple layout, two Buttons and a TextView. Of note, the TextView is contained within a ScrollView. The reason for this is that our data extracted from the JSON file is more than a screen-ful, so we want the user to have an easy means of accessing all of the data. The ScrollView takes care of all of this scrolling for us. Note also the use of a LinearLayout with a horizontal orientation to hold the two Buttons. Here is the main.xml file from the project.

<?xml version="1.0" encoding="utf-8"?&rt

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    &rt

<LinearLayout
	android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
	   &rt

<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnTest" android:text="Test JSON"&rt</Button&rt
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnTestFile" android:text="Parse JSON file"&rt</Button&rt
</LinearLayout&rt

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ScrollView01" android:layout_width="fill_parent" android:layout_height="wrap_content"&rt

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text=""
    android:layout_gravity="center_horizontal"
    android:id="@+id/txtStatus"
    android:isScrollContainer="true"/&rt

</ScrollView&rt
</LinearLayout&rt

Parsing JSON

So, how do we parse JSON? Simple, we use the constructor of the JSONObject() which takes a string. In the first example we use a statically defined Java String which contains a simple JSON object.

String staticObject = "{\"firstname\":\"Steve\",\"lastname\":\"Jobs\",\"cellphones\":\"0\"}";

void buildObject()
{
	try
	{
		obj = new JSONObject(staticObject);
		String x = obj.get("firstname").toString() + " " + obj.get("lastname").toString() + " has " + obj.getInt("cellphones") + " Android phones.";
		setStatus(x);
	}
	catch (JSONException je)
	{
		setStatus("Error occured " + je.getMessage());
	}
}

void setStatus(String x)
{
	TextView tv = (TextView) findViewById(R.id.txtStatus);
	tv.setText(x);
}

This code takes the following steps:

  • Creates a new JSONObject, passing in a String representing a JSON object.
  • Constructs a String (x) by extracting elements from the parsed JSON object.
  • Setting the contents of a TextView with the constructed String.

Here is a screen shot of this code running.

Simple JSON parsing
Simple JSON parsing

The second JSON example parses “raw” resource containing a representation of the Kawasaki/Stearns interview.

The interview is stored as a text file under the “raw” folder beneath the res folder within the Android project. Note that the raw folder is not created by default by the Android Developer Tools — you need to create that yourself. Here is a screen shot of the Android project.

The interview in the Android project
The interview in the Android project

Here is the code used to read in the resource, parse it and construct a string to view the data. Note that we could represent this data as something other than a string — and we’ll take a look at that in a future article.

void buildObjectFromFile()
{
	try
	{
		String x = "";
		InputStream is = this.getResources().openRawResource(R.raw.interview);
		byte [] buffer = new byte[is.available()];
		while (is.read(buffer) != -1);
		String json = new String(buffer);
		obj = new JSONObject(json);
		x = obj.getString("firstname") + " " + obj.getString("lastname") + "\n";
		x += obj.getString("occupation") + "\n";

		JSONObject interview =  obj.getJSONObject("interview");
		x += "Interview source:" + interview.getString("source")  + "\n";

		JSONArray questions = interview.getJSONArray("questions");
		x += "Number of questions: " + questions.length()  + "\n\n";

		int i;
		for (i=0;i<questions.length();i++)
		{
			JSONObject qa = questions.getJSONObject(i);
			x += "------------\n";
			x += "Q" + (i+1) + ". " + qa.getString("Question") + "\n\n";
			x += "A" + (i+1) + ". " + qa.getString("Answer") + "\n";
		}
		setStatus(x);
	}
	catch (Exception je)
	{
		setStatus("Error w/file: " + je.getMessage());
	}
}

Here are a few things to note about this code sample.

  • We access the resource with a call to getResources().openRawResource(…), using the auto-generated identifier R.raw.interview.
  • After reading in the data and converting it to a java.lang.String, we parse it in the constructor of the JSONObject.
  • Extracting the interview object is accomplished by calling getJSONObject(), in the same way we extract a String with getString(), except in this case a JSONObject is returned instead of a String.
  • Extracting the questions “ordered list/collection” is accomplished with a call to getJSONArray(). Each element of the array is a JSONObject itself.
JSONArray questions = interview.getJSONArray("questions");

We extract the number of objects in the questions array via the length() method.

Next, we iterate through the questions, formatting a string and then wrap up by setting the value of the TextView with our formatted interview text.

The interview questions
The interview questions

Admittedly, the GUI isn’t very exciting — we need to make better use of the available Android UI widgets — a task we’ll undertake in an upcoming article.

And by the way, if you have ever wondered just what one person can do to make “the world a better place”, consider lending a hand to a child through World Vision.

Comments on "To XML and Back: Using JSON in Android"

otaibinm

I am sorry but JSON is no way near competing with XML. XML is far superior to JSON!! I don\’t really know on what grounds is the author basing his conclusions!

Reply
fableson

@otalbinm

Thanks for your comment — everyone is entitled to their opinion. Let\’s discuss — what are your thoughts on why XML is superior?

In my view, XML is a super-set of encodings in many respects — but can be excessive in \”weight\” in typical, tag-heavy usage. JSON is a better fit for some places where CDATA elements are not needed, straight-forward structures are used (or somewhat complex, nested structures also …), and where you have a JavaScript environment where you would like to \”eval()\” the data, thereby instantiating objects. This can be easier navigated than having to parse an XML structure. While it\’s not for every scenario and every use, JSON is versatile. The mere fact that it is showing up in additional places as alternatives to traditional API\’s is reason enough to give it some consideration.

Frank

Reply
ddennedy

Great. It appears the herd is deciding to trade off verbose, repetitive tags for this nasty morass of quotation marks, quote-escaping, matching braces and brackets, and stray commas. I thought it was bad practice to just eval it negating much of its convenience trump. I am taking a liking toward YAML Tiny for which I can write a parser and serializer where needed without taking a sabbatical.

Reply
sebastian.ricaldoni

I believe we are missing the point here. I don\’t think Frank is saying that XML should be erased from the map and replaced with JSON. IMHO JSON is yet another way of doing the same thing, which is, communicating. I agree that JSON will be more suitable than XML in certain scenarios. However, this doesn\’t imply that XML should disappear. On the contrary, XML is the natural evolution of proprietary communication protocols. At the end, I believe that as \’technology experts\’ we should always keep our minds wide open to new suggestions and different approaches to accomplish the same thing.
Regards,
Sebastian

Reply

the best thing about RESTful web services is that they are supposed to be free of any representation convention. So its possible to chose the most suitable format for any specific case on the fly (e.g. JSON for mobile apps and XML for business SOA environments..)

Reply

You are in reality a excellent webmaster. The web site loading speed is amazing. It seems that you’re doing any unique trick. Also, The contents are masterwork. you have performed a great job in this matter!

Reply

how to integrate forms in android

Reply

Hello are using WordPress for your blog platform? I’m new to the blog world but I’m trying to get started and create my own. Do you require any coding expertise to make your own blog? Any help would be greatly appreciated!

Reply

When it’s very sunny, The ppi Lumia 920 manages
to keep a few guidelines in mind to prevent similar occurrences in the future when new technology gives us better
storage options. The word pixel comes from picture element.

In many cases, did not ppi want or need it. This condition,
which is worthless. The ppi proportion settled in favour of the customer in mind.
If you have bought this kind of activity is known as the penetration target.

Feel free to surf to my homepage: best ppi claim company (Mildred)

Reply

Thank you for the good writeup. It in fact was once a amusement account it.
Look complex to far brought agreeable from you!
By the way, how can we be in contact?

My web site Usfastcash.com

Reply

Heya i am for the first time here. I found this board and I to find It really helpful & it helped me out much.
I hope to provide something again and help others such as you helped me.

My web site … what happens if you don’t pay a payday loan

Reply

I like what you guys are up also. Such intelligent work and reporting! Keep up the excellent works guys I’ve incorporated you guys to my blogroll. I think it’ll improve the value of my website :)

Reply

Good – I should certainly pronounce, impressed with your site. I had no trouble navigating through all tabs as well as related info ended up being truly simple to do to access. I recently found what I hoped for before you know it in the least. Reasonably unusual. Is likely to appreciate it for those who add forums or anything, website theme . a tones way for your customer to communicate. Nice task..

Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>