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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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
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

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

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

  • Immutable Secrets Management: A Zero-Trust Approach to Sensitive Data in Containers
  • Orchestrating Microservices with Dapr: A Unified Approach
  • A Simple, Convenience Package for the Azure Cosmos DB Go SDK
  • Manual Sharding in PostgreSQL: A Step-by-Step Implementation Guide
  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.2K 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.

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
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!