Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Deserializing Json to a Java Object Using Google’s Gson Library

DZone's Guide to

Deserializing Json to a Java Object Using Google’s Gson Library

· Java Zone
Free Resource

Make it happen: rapid app development on Kubernetes as a managed service.

Javascript object notation (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 JSON 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-rpcGsonFlexjson 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());
}
  }

Tutorial: WordPress as distributed microservices on Kubernetes.

Topics:
java ,tips and tricks

Published at DZone with permission of Ayobami Adewole, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}