INTRODUCTION
This article will show you how JSON conversion can be done using Gson Library.
Gson is a Google's Open source Library
Gson can convert to JSON of
- Any Collection Object (List, Map, Set)
- Any User defined Object
SOFTWARES & TOOLS
- Eclipse IDE (Mars2)
- gson-1.2.2.jar or any latest gson jar.
IMPLEMENTATION
Create a POJO class for person.
package jbr.jsonex;
public class Employee {
private String empId;
private String firstName;
private String lastName;
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
private Address address;
}
|
Create another POJO for address.
package jbr.jsonex;
public class Address {
Address(String streetName, String doorNo, String city, String state, String country) {
this.streetName = streetName;
this.doorNo = doorNo;
this.city = city;
this.state = state;
this.country = country;
}
private String streetName;
private String doorNo;
public String getStreetName() {
return streetName;
}
public void setStreetName(String streetName) {
this.streetName = streetName;
}
public String getDoorNo() {
return doorNo;
}
public void setDoorNo(String doorNo) {
this.doorNo = doorNo;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
private String city;
private String state;
private String country;
}
|
Create a client program to test it.
package jbr.jsonex;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import com.google.gson.Gson;
public class JsonUsingGson {
public static void main(String[] args) {
convertCollectionToJson();
convertObjectToJson();
}
public static void convertCollectionToJson() {
Map<String, Object> map = new TreeMap<>();
map.put("EmpId", "100");
map.put("FirstName", "Ranjith");
map.put("LastName", "Sekar");
map.put("Address", "Chennai");
List<String> mobNo = new ArrayList<String>();
mobNo.add("9545454");
mobNo.add("3434234");
map.put("mobile", mobNo);
Gson gson = new Gson();
System.out.println(gson.toJson(map));
}
public static void convertObjectToJson() {
Employee employee = new Employee();
employee.setEmpId("100");
employee.setFirstName("Ranjith");
employee.setLastName("Sekar");
employee.setAddress(new Address("Nehru street", "123", "chennai", "tamilnadu", "india"));
Gson gson = new Gson();
System.out.println(gson.toJson(employee));
}
}
|
OUTPUT
{"Address":"Chennai","EmpId":"100","FirstName":"Ranjith","LastName":"Sekar","mobile":["9545454","3434234"]}
{"empId":"100","firstName":"Ranjith","lastName":"Sekar","address":{"streetName":"Nehru street","doorNo":"123","city":"chennai","state":"tamilnadu","country":"india"}}
|
No comments :
Post a Comment