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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • A React Frontend With Go/Gin/Gorm Backend in One Project
  • From On-Prem to SaaS
  • Cucumber Selenium Tutorial: A Comprehensive Guide With Examples and Best Practices
  • Performance Comparison — Thread Pool vs. Virtual Threads (Project Loom) In Spring Boot Applications

Trending

  • A React Frontend With Go/Gin/Gorm Backend in One Project
  • From On-Prem to SaaS
  • Cucumber Selenium Tutorial: A Comprehensive Guide With Examples and Best Practices
  • Performance Comparison — Thread Pool vs. Virtual Threads (Project Loom) In Spring Boot Applications
  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

Ayobami Adewole user avatar by
Ayobami Adewole
·
Aug. 27, 14 · Tutorial
Like (0)
Save
Tweet
Share
84.46K 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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • A React Frontend With Go/Gin/Gorm Backend in One Project
  • From On-Prem to SaaS
  • Cucumber Selenium Tutorial: A Comprehensive Guide With Examples and Best Practices
  • Performance Comparison — Thread Pool vs. Virtual Threads (Project Loom) In Spring Boot Applications

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: