JSON parsing with Gson

This is an example of JSON parsing on Android with Gson. Google Gson is a Java library that can be used to convert Java Objects into their JSON representation.

In order to parse JSON with Gson, follow these steps:

  • Create a class that extends Activity (here called JsonParsingActivity)
  • Create an instance of the Gson class
  • Use the DefaultHttpClient to retrieve the data if this is a web resource
  • Create a class that will represent the model of the input (here called SearchResponse)
  • Use the fromJson method in order to parse the JSON input and return the model object

These are demonstrated in the code snippet(s) below:

package com.javacodegeeks.android.json;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.javacodegeeks.android.json.model.Result;
import com.javacodegeeks.android.json.model.SearchResponse;

public class JsonParsingActivity extends Activity {

    String url = "http://search.twitter.com/search.json?q=javacodegeeks";

    public void onCreate(Bundle savedInstanceState) {



  InputStream source = retrieveStream(url);

  Gson gson = new Gson();

  Reader reader = new InputStreamReader(source);

  SearchResponse response = gson.fromJson(reader, SearchResponse.class);

  Toast.makeText(this, response.query, Toast.LENGTH_SHORT).show();

  List<Result> results = response.results;

  for (Result result : results) {

Toast.makeText(this, result.fromUser, Toast.LENGTH_SHORT).show();



    private InputStream retrieveStream(String url) {

  DefaultHttpClient client = new DefaultHttpClient(); 

  HttpGet getRequest = new HttpGet(url);

  try {

     HttpResponse getResponse = client.execute(getRequest);

     final int statusCode = getResponse.getStatusLine().getStatusCode();

     if (statusCode != HttpStatus.SC_OK) { 


"Error " + statusCode + " for URL " + url); 

  return null;


     HttpEntity getResponseEntity = getResponse.getEntity();

     return getResponseEntity.getContent();


  catch (IOException e) {


     Log.w(getClass().getSimpleName(), "Error for URL " + url, e);


  return null;


package com.javacodegeeks.android.json.model;

import java.util.List;

import com.google.gson.annotations.SerializedName;

public class SearchResponse {

    public List results;

    public long maxId;

    public int sinceId;

    public String refreshUrl;

    public String nextPage;

    public int resultsPerPage;

    public int page;

    public double completedIn;

    public String sinceIdStr;

    public String maxIdStr;

    public String query;

package com.javacodegeeks.android.json.model;

import com.google.gson.annotations.SerializedName;

public class Result {

    public String fromUserIdStr;

    public String profileImageUrl;

    public String createdAt;

    public String fromUser;

    public String idStr;

    public Metadata metadata;

    public String toUserId;

    public String text;

    public long id;

    public String from_user_id;

    public String isoLanguageCode;

    public String toUserIdStr;

    public String source;

package com.javacodegeeks.android.json.model;

import com.google.gson.annotations.SerializedName;

public class Metadata {

    public String resultType;


This was an example of how to parse JSON with Gson on Android.

Related Article:

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button