Deserializing Json to a Java Object using Google’s Gson library



Javascript object notations (Json) is fast becoming the de facto standard or format for transferring, sharing and passing around data. Be it on the web, REST Service, a remote procedure call or even an ajax request. Json is light weight with little memory footprint when compared to an xml.

The content of a Json string in its raw form when observed looks gibberish. To make the content usable it needs to be deserialized or converted to a useable form usually a java object (POJO) or an array or list of objects depending on the jscon content.

A typical json string is as shown below

{"city":"Jos","country":"Nigeria","houseNumber":"13","lga":"Jos South",
         "state":"Plateau","streetName":"Jonah Jann","village":"Bukuru","ward":"1"}
            


There are a lot of frameworks for deserializing json to a java object such as json-rpc, Gson, Flexjson and a whole lots of other open source libraries. Of all the libraries mentioned I would in this blog post demonstrate how to use google-Gson library to deserialize a json string to a java object. You can download the Gson library from https://code.google.com/p/google-gson/.

To have the json string deserialized, a java object must be created that has the same fields names with the fields in the json string. There is a website that provides a service for viewing the content of a json string in a tree like manner. http://jsonviewer.stack.hu

Paste the json string in the text tab



And view the fields and the content from the Viewer tab



I would deserialize a json string that contains address details to an Address POJO, the address object follows the structure as seen from the json tree view above.

public class Address{	
	
	private String city;

	private String country;

	private String houseNumber;	
	
	private String lga;

	private String state;

	private String streetName;
		
	private String village;
	
	private String ward;
	
	
	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	
	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getHouseNumber() {
		return houseNumber;
	}

	public void setHouseNumber(String houseNumber) {
		this.houseNumber = houseNumber;
	}
	

	public String getLga() {
		return lga;
	}

	public void setLga(String lga) {
		this.lga = lga;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getStreetName() {
		return streetName;
	}

	public void setStreetName(String streetName) {
		this.streetName = streetName;
	}
	public String getVillage() {
		return village;
	}

	public void setVillage(String village) {
		this.village = village;
	}

	public String getWard() {
		return ward;
	}

	public void setWard(String ward) {
		this.ward = ward;
	}
	
	@Override
	public String toString() {
		return "Address [city=" + city + ", country=" + country
				+ ", houseNumber=" + houseNumber + ", lga=" + lga + ", state="
				+ state + ", streetName=" + streetName + ", village=" + village
				+ ", ward=" + ward + "]";
	}
}	
            


To perform the deserialization with Gson is easy, create POJO classes to hold your data, import the packages com.google.gson.Gson and com.google.gson.GsonBuilder, to your project. Then create and instance of the Gson class and then perform the deserialization as shown below.
Gson gson = new GsonBuilder().create();
Address address=gson.fromJson(json, Address.class);
            


Voila, you have your json deserialized!

The source code listing is below.

package jsondeserializer

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

  public class Tester {
	public static void main(String[] args) {
		String json ="{\"city\":\"Jos\",\"country\":\"Nigeria\",\"houseNumber\":\"13\",\"lga\":\"Jos South\",\n" + 
				"\"state\":\"Plateau\",\"streetName\":\"Jonah Jann\",\"village\":\"Bukuru\",\"ward\":\"1\"}";
		Gson gson = new GsonBuilder().create();
		Address address=gson.fromJson(json, Address.class);
		System.out.println(address.toString());
	}
  }
        




Share this page on


  0 People Like(s) This Page   Permalink  

 Click  To Like This Page

comments powered by Disqus

page