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:

  1. Add jar files of Jackson (in case of Maven project add Gson dependencies in the pom.xml file)
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <dependency>
           <groupId>com.google.code.gson</groupId>
           <artifactId>gson</artifactId>
           <version>2.6.2</version>
       </dependency>

    chevron_right

    
    

    Below is the screenshot showing this step:-

  2. Create a POJO (Plain Old Java Object) to be converted into JSON
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    package GeeksforGeeks.Geeks;
    public class Organisation {
        private String organisation_name;
        private String description;
        private int Employees;
      
        // Calling getters and setters
        public String getOrganisation_name()
        {
            return organisation_name;
        }
        public void setOrganisation_name(String organisation_name)
        {
            this.organisation_name = organisation_name;
        }
        public String getDescription()
        {
            return description;
        }
        public void setDescription(String description)
        {
            this.description = description;
        }
        public int getEmployees()
        {
            return Employees;
        }
        public void setEmployees(int employees)
        {
            Employees = employees;
        }
      
        // Creating toString
        @Override
        public String toString()
        {
            return "Organisation [organisation_name="
                + organisation_name
                + ", description="
                + description
                + ", Employees="
                + Employees + "]";
        }
    }

    chevron_right

    
    

    Below is the screenshot showing this step:-

  3. 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\” }

  4. Create a Java class for converting the Json into Organisation object:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    package GeeksforGeeks.Geeks;
      
    import com.google.gson.Gson;
      
    public class JsonToObject {
      
        public static void main(String[] args)
        {
            // Creating object of Organisation
            Organisation org = new Organisation();
      
            // Converting json to object
            org = getOrganisationObject();
      
            // Displaying object
            System.out.println(org);
        }
      
        private static Organisation getOrganisationObject()
        {
            // Storing preprocessed json(Added slashes)
            String OrganisationJson
                = "{\"organisation_name\" 
                : \"GeeksforGeeks\",
                \"description\" 
                : \"A computer Science portal for Geeks\",
                \"Employee\" 
                : \"2000\"}";
      
            // Creating a Gson Object
            Gson gson = new Gson();
      
            // Converting json to object
            // first parameter should be prpreocessed json
            // and second should be mapping class
            Organisation organisation
                = gson.fromJson(OrganisationJson,
                                Organisation.class);
      
            // return object
            return organisation;
        }
    }

    chevron_right

    
    

    Below is the screenshot showing this step:-

  5. Execute the process
  6. Output:

    Organisation [organisation_name=GeeksforGeeks, description=A computer Science portal for Geeks, Employees=0]



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.