JSON string to java object using com.google.gson.Gson in java

Maven dependency -

<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>


If you are beginner don't worry learn how to Create new Maven project - In 2 minutes

c:/Ankit/myJson.json >

{"empId":11,"name":"ankit","address":["Paris","London"],"birthDate":"Sep 2, 2018 12:59:30 PM"}

Convert - JSON string to java object or JSON to java string

package com.ankit.json;

import java.io.FileReader;

import com.google.gson.Gson;
import com.google.gson.JsonElement;

public class ConvertJsonToJavaObject {

    public static void main(String[] args) {
          try {
                 Gson gsonnew Gson();

                 // 1 - Convert JSON to JsonElement
                 JsonElement jsonElementgson.fromJson(
                        "{\"empId\":11,\"name\":\"ankit\",\"address\":[\"Paris\",\"London\"],\"birthDate\":\"Sep 2, 2018 12:59:30 PM\"}",
                              JsonElement.class);
                 String jsonInStringgson.toJson(jsonElement); // Convert JsonElement to String
                 System.out.println("jsonInString = "jsonInString);

                
                 //2 - JSON to java object
                 FileReader fileReadernew FileReader("c:/Ankit/myJson.json");
                 Employee empgson.fromJson(fileReader, Employee.class);
                 System.out.println("emp = "+emp);

          catch (Exception e) {
                 e.printStackTrace();
          }
    }

}

//output
/*

jsonInString = {"empId":11,"name":"ankit","address":["Paris","London"],"birthDate":"Sep 2, 2018 12:59:30 PM"}
emp = Employee [empId=11, name=ankit, address=[ParisLondon], birthDate=Sun Sep 02 12:59:30 IST 2018]

*/

Employee class

package com.ankit.json;

import java.util.Date;
import java.util.List;

public class Employee {

    //Fields
   
    private int empId;
   
    private String name;

    private List<String> address;
   
    private Date birthDate;


    public Employee() {
          super();
    }
   
    public Employee(int empId, String name, List<String> address, Date birthDate) {
          super();
          this.empIdempId;
          this.namename;
          this.addressaddress;
          this.birthDate=birthDate;
    }
   
    @Override
    public String toString() {
          return "Employee [empId="empId", name="name", address="address", birthDate="birthDate"]";
    }
   

    //Setter and getters
    public int getEmpId() {
          return empId;
    }
    public void setEmpId(int empId) {
          this.empIdempId;
    }
    public String getName() {
          return name;
    }
    public void setName(String name) {
          this.namename;
    }
    public List<String> getAddress() {
          return address;
    }
    public void setAddress(List<String> address) {
          this.addressaddress;
    }
   
    public Date getBirthDate() {
          return birthDate;
    }

    public void setBirthDate(Date birthDate) {
          this.birthDatebirthDate;
    }

}




Related links >

1. Jackson JSON -

We can use Jackson api for for processing JSON in java.

Jackson JSON examples


2. Java provides API (JSR 353) for Processing  JSON (JSR 353).
It provides -
  • Object Model API
  • Streaming API

Java API for JSON processing examples >



3. Simple  (simple.JSONObject)



4. Google Gson - processing java json


eEdit
Must read for you :