DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Generics in Java and Their Implementation
  • High-Performance Java Serialization to Different Formats
  • Writing DTOs With Java8, Lombok, and Java14+
  • Redefining Java Object Equality

Trending

  • Introduction to Tactical DDD With Java: Steps to Build Semantic Code
  • 5 Common Security Pitfalls in Serverless Architectures
  • Chaos Engineering Has a Blind Spot. Agentic AI Lives in It.
  • Implementing Observability in Distributed Systems Using OpenTelemetry
  1. DZone
  2. Coding
  3. Languages
  4. Deserializing Json to a Java Object Using Google’s Gson Library

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

By 
Ayobami Adewole user avatar
Ayobami Adewole
·
Aug. 27, 14 · Tutorial
Likes (0)
Comment
Save
Tweet
Share
90.8K Views

Join the DZone community and get the full member experience.

Join For Free
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-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());
}
  }

Object (computer science) Gson Java (programming language) Library Strings Data Types

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

Opinions expressed by DZone contributors are their own.

Related

  • Generics in Java and Their Implementation
  • High-Performance Java Serialization to Different Formats
  • Writing DTOs With Java8, Lombok, and Java14+
  • Redefining Java Object Equality

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook