Tuesday, November 30, 2010

JSON Parsing Example

1. Create an Android application with one activity, name it as you wish, if you are doing it in eclipse all the basic stuff will be generated for you which is exactly what we need.
2. Find an example url which responds in JSON, I have used Twitter Trends found here: http://search.twitter.com/trends.json
The example response looks like this:
{"as_of":"Thu, 25 Feb 2010 11:30:17 +0000","trends":[{"name":"#nowplaying","url":"http://search.twitter.com/search?q=%23nowplaying"},{"name":"#nothingworsethan","url":"http://search.twitter.com/search?q=%23nothingworsethan"},{"name":"Dubai Mall","url":"http://search.twitter.com/search?q=%22Dubai+Mall%22"},{"name":"iPad Gets","url":"http://search.twitter.com/search?q=%22iPad+Gets%22"},{"name":"#SuperJuniorTrot","url":"http://search.twitter.com/search?q=%23SuperJuniorTrot"},{"name":"Justin Bieber","url":"http://search.twitter.com/search?q=%22Justin+Bieber%22"},{"name":"Click","url":"http://search.twitter.com/search?q=Click"},{"name":"Jaebum","url":"http://search.twitter.com/search?q=Jaebum"},{"name":"#tosavemoney","url":"http://search.twitter.com/search?q=%23tosavemoney"},{"name":"Protection","url":"http://search.twitter.com/search?q=Protection"}]}

As you can see this json data contains date attribute called ‘as_of’ as well as array of items, each consisting of name and url attributes.
We will create two simple java classes wich will hold that information.
First one is TwitterTrends.java
package com.softwarepassion.jsonexample;

import java.util.Date;
import java.util.List;

public class TwitterTrends {

private String as_of;
private List trends;

public String getAs_of() {
return as_of;
public void setAs_of(String asOf) {
as_of = asOf;
public List getTrends() {
return trends;
public void setTrends(List trends) {
this.trends = trends;

And the second one is a simple TwitterTrend.java
package com.softwarepassion.jsonexample;

public class TwitterTrend {
private String name;
private String url;

public String getName() {
return name;
public void setName(String name) {
this.name = name;
public String getUrl() {
return url;
public void setUrl(String url) {
this.url = url;
TwitterTrends contains a list of TwitterTrend objects, both of them are simple beans and are very similar to the json response if you look closely
Now the fun part begins.
Download the Gson library from http://code.google.com/p/google-gson/ and add it to your java build path in eclipse.
Once you do that, you can now transform your json response to Java object on Android Platform:
Get the response as InputStream:

public InputStream getJSONData(String url){
DefaultHttpClient httpClient = new DefaultHttpClient();
URI uri;
InputStream data = null;
try {
uri = new URI(url);
HttpGet method = new HttpGet(uri);
HttpResponse response = httpClient.execute(method);
data = response.getEntity().getContent();
} catch (Exception e) {

return data;
Then use that input stream to create your java objects:
public void runJSONParser(){
Log.i("MY INFO", "Json Parser started..");
Gson gson = new Gson();
Reader r = new InputStreamReader(getJSONData("http://search.twitter.com/trends.json"));
Log.i("MY INFO", r.toString());
TwitterTrends objs = gson.fromJson(r, TwitterTrends.class);
Log.i("MY INFO", ""+objs.getTrends().size());
for(TwitterTrend tr : objs.getTrends()){
Log.i("TRENDS", tr.getName() + " - " + tr.getUrl());
}catch(Exception ex){

1 comment: