GSON is the
library that can be used to convert Java objects in to their JSON
representation.It can also be used to convert a JSON string to an
equivalent Java object.
- Gson can work with arbitrary Java objects including pre-existing objects that you do not have source- code of.
- Provide easy to use mechanisms like toString() and constructor (factory method) to convert Java to JSON and vice-versa.
- Allow pre-existing unmodifiable objects to be converted to and from JSON
- Allow custom representations for objects
- Generate compact and readability JSON output
import java.util.ArrayList;
public class DataObject {
private ArrayList<Employee> empList = new ArrayList<Employee>();
public ArrayList<Employee> getEmpList() {
return empList;
}
public void setEmpList(ArrayList<Employee> empList) {
this.empList = empList;
}
public String toString(){
StringBuffer employeeList = new StringBuffer();
String newLine = System.getProperty("line.separator");
employeeList.append("Employee List ::");
employeeList.append(newLine);
for(Employee emp : empList ){
employeeList.append(emp.toString());
employeeList.append(newLine);
}
return employeeList.toString();
}
}
Employee.java
public class Employee {
private String empId;
private String empName;
private String empAge;
private String empSal;
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getEmpAge() {
return empAge;
}
public void setEmpAge(String empAge) {
this.empAge = empAge;
}
public String getEmpSal() {
return empSal;
}
public void setEmpSal(String empSal) {
this.empSal = empSal;
}
public String toString(){
StringBuffer empDetails = new StringBuffer();
String newLine = System.getProperty("line.separator");
empDetails.append("Employee Details ::");
empDetails.append(newLine);
empDetails.append("Emp Id : " + empId);
empDetails.append(newLine);
empDetails.append("Emp Age : " + empAge);
empDetails.append(newLine);
empDetails.append("Emp Name : " + empName);
empDetails.append(newLine);
empDetails.append("Emp Sal : " + empSal);
empDetails.append(newLine);
return empDetails.toString();
}
}
GsonExample.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import com.google.gson.Gson;
/**
*
*
* GSONExample is a Java Class which will give the example to convert
* 1. Java Object to Json Object
* 2. Json Object to Java Object
*
* Gson - Gson is a Java library that can be used to convert Java Objects into their JSON representation.
* It can also be used to convert a JSON string to an equivalent Java object
*/
public class GSONExample {
public static void main(String[] args) {
Gson gson = new Gson();
// Form the Data object
DataObject employees = formJavaObject();
// Convert Object to json String
String json = gson.toJson(employees);
System.out.println(json);
//Convert Json String to Java object
DataObject obj = gson.fromJson(json, DataObject.class);
System.out.println(obj.toString());
// To store the Json Value to File called emp.json
setJsonValueToFile();
//To get the Json String from emp.Json file and convert into JavaObject
getJsonValueFromFile();
}
/**
* formJavaObject - To form the Employee data Object
* @return DataObject
*/
private static DataObject formJavaObject() {
DataObject dataObject = new DataObject();
ArrayList empList = new ArrayList();
Employee emp1 = new Employee();
emp1.setEmpId("379023");
emp1.setEmpAge("23");
emp1.setEmpName("Kanchu");
emp1.setEmpSal("10000");
Employee emp2 = new Employee();
emp2.setEmpId("111111");
emp2.setEmpAge("23");
emp2.setEmpName("Lakshman");
emp2.setEmpSal("10000");
empList.add(emp1);
empList.add(emp2);
dataObject.setEmpList(empList);
System.out.println(dataObject.toString());
return dataObject;
}
/**
* setJsonValueToFile - To store the Json String to emp.json file
*/
private static void setJsonValueToFile() {
DataObject dataObject = formJavaObject();
Gson gson = new Gson();
String json = null;
try {
// Convert Object to Json String
json = gson.toJson(dataObject);
// Write json data to a file named "emp.json"
FileWriter writer = new FileWriter("C:\\Laxman\\emp.json");
writer.write(json);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(json);
}
/**
* getJsonValueFromFile - To get the Json String from emp.json and convert to Employee data object
*/
private static void getJsonValueFromFile() {
Gson gson = new Gson();
try {
BufferedReader br = new BufferedReader(new FileReader("C:\\Laxman\\emp.json"));
// Convert Json String to Java Object
DataObject empListObj = gson.fromJson(br, DataObject.class);
System.out.println(empListObj);
} catch (IOException e) {
e.printStackTrace();
}
}
}
OutPut ::
Employee List ::
Employee Details ::
Emp Id : 999999
Emp Age : 23
Emp Name : Raj
Emp Sal : 10000
Employee Details ::
Emp Id : 111111
Emp Age : 23
Emp Name : ABC
Emp Sal : 10000
{"empList":[{"empId":"999999","empName":"Raj","empAge":"23","empSal":"10000"},{"empId":"111111","empName":"ABC","empAge":"23","empSal":"10000"}]}
Employee List ::
Employee Details ::
Emp Id : 999999
Emp Age : 23
Emp Name : Raj
Emp Sal : 10000
Employee Details ::
Emp Id : 111111
Emp Age : 23
Emp Name : ABC
Emp Sal : 10000
Employee List ::
Employee Details ::
Emp Id : 999999
Emp Age : 23
Emp Name : Raj
Emp Sal : 10000
Employee Details ::
Emp Id : 111111
Emp Age : 23
Emp Name : ABC
Emp Sal : 10000
{"empList":[{"empId":"999999","empName":"Raj","empAge":"23","empSal":"10000"},{"empId":"111111","empName":"ABC","empAge":"23","empSal":"10000"}]}
Employee List ::
Employee Details ::
Emp Id : 999999
Emp Age : 23
Emp Name : Raj
Emp Sal : 10000
Employee Details ::
Emp Id : 111111
Emp Age : 23
Emp Name : ABC
Emp Sal : 10000