Convert Json String to Java Object Using GSON
Pre-erquisite: Convert Java Object to Json String Using GSON
JSON Stand for JavaScript Object Notation. It’s a standard text-based format which shows structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications. JSON is highly recommended to transmit data between a server and web application.
To convert a Java object into JSON, the following methods can be used:
- GSON: It is an open-source Java library which is used to serialize and deserialize Java objects to JSON.
- Jackson API
In this article, a predefined JSON String is converted into Java Object using GSON.
Examples:
Input:
{
“organisation_name” : “GeeksforGeeks”,
“description” : “A computer Science portal for Geeks”,
“Employee” : “2000”
}
Output:
Organisation [organisation_name=GeeksforGeeks, description=A computer Science portal for Geeks, Employees=0]Input:
{
“Student_name” : “XYZ”,
“Organisation_name” : “GeeksForGeeks”
“Roll_No” : “1”
}
Output:
Student [Student_name=XYZ, Organisation_name=GeeksForGeeks, Roll_no=1]
The steps to do this are as follows:
- Add jar files of Jackson (in case of Maven project add Gson dependencies in the pom.xml file)
<dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId><version>2.6.2</version></dependency>chevron_rightfilter_noneBelow is the screenshot showing this step:-
- Create a POJO (Plain Old Java Object) to be converted into JSON
packageGeeksforGeeks.Geeks;publicclassOrganisation {privateString organisation_name;privateString description;privateintEmployees;// Calling getters and setterspublicString getOrganisation_name(){returnorganisation_name;}publicvoidsetOrganisation_name(String organisation_name){this.organisation_name = organisation_name;}publicString getDescription(){returndescription;}publicvoidsetDescription(String description){this.description = description;}publicintgetEmployees(){returnEmployees;}publicvoidsetEmployees(intemployees){Employees = employees;}// Creating toString@OverridepublicString toString(){return"Organisation [organisation_name="+ organisation_name+", description="+ description+", Employees="+ Employees +"]";}}chevron_rightfilter_noneBelow is the screenshot showing this step:-
- Create a String Variable for Storing Json String:
Note: This Json string should not be a simple Json String. Preprocess the JSON String and add slashes before passing it into GSON object.Example of Preprocessing:
Intial JSON String:
{“organisation_name” : “GeeksforGeeks”, “description” : “A computer Science portal for Geeks”, “Employee” : “2000”}Preprocessed JSON String:
{ \”organisation_name\” : \”GeeksforGeeks\”, \”description\” : \”A computer Science portal for Geeks\”, \”Employee\” : \”2000\” } - Create a Java class for converting the Json into Organisation object:
packageGeeksforGeeks.Geeks;importcom.google.gson.Gson;publicclassJsonToObject {publicstaticvoidmain(String[] args){// Creating object of OrganisationOrganisation org =newOrganisation();// Converting json to objectorg = getOrganisationObject();// Displaying objectSystem.out.println(org);}privatestaticOrganisation getOrganisationObject(){// Storing preprocessed json(Added slashes)String OrganisationJson= "{\"organisation_name\": \"GeeksforGeeks\",\"description\": \"A computer Science portalforGeeks\",\"Employee\": \"2000\"}";// Creating a Gson ObjectGson gson =newGson();// Converting json to object// first parameter should be prpreocessed json// and second should be mapping classOrganisation organisation= gson.fromJson(OrganisationJson,Organisation.class);// return objectreturnorganisation;}}chevron_rightfilter_noneBelow is the screenshot showing this step:-

- Execute the process
- Output:
Organisation [organisation_name=GeeksforGeeks, description=A computer Science portal for Geeks, Employees=0]
Recommended Posts:
- Convert Java Object to Json String using GSON
- Convert Java Object to Json String using Jackson API
- How to Convert JS Object to JSON String in JQuery/Javascript?
- Python | Ways to convert string to json object
- JSON | modify an array value of a JSON object
- Deserializing a JSON into a JavaScript object
- JavaScript | How to add an element to a JSON object?
- JavaScript | Add new attribute to JSON object
- JavaScript | Convert an array to JSON
- How to convert PHP array to JavaScript or JSON ?
- Converting JSON text to JavaScript Object
- How to send a JSON object to a server using Javascript?
- JavaScript | Check if a key exists inside a JSON object
- How to convert JSON data to a html table using JavaScript/jQuery ?
- Convert a List of String to a comma separated String in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



