This
annotation indicates this member should be serialized to JSON with the provided
name value as its field name.
public class Address { private String street; private String city; private String state; private String country; private String PIN; public String getPIN() { return PIN; } public String getCity() { return city; } public String getCountry() { return country; } public String getState() { return state; } public String getStreet() { return street; } public void setPIN(String PIN) { this.PIN = PIN; } public void setCity(String city) { this.city = city; } public void setCountry(String country) { this.country = country; } public void setState(String state) { this.state = state; } public void setStreet(String street) { this.street = street; } }
import java.util.*; import com.google.gson.annotations.SerializedName; public class Employee { @SerializedName("empId") private int id; @SerializedName("fName") private String firstName; @SerializedName("lName") private String lastName; private String designation; private String mailId; private List<String> hobbies; @SerializedName("address") private Address addr; public String getDesignation() { return designation; } public int getId() { return id; } public void setDesignation(String designation) { this.designation = designation; } public void setId(int id) { this.id = id; } public Address getAddr() { return addr; } public String getFirstName() { return firstName; } public List<String> getHobbies() { return hobbies; } public String getLastName() { return lastName; } public String getMailId() { return mailId; } public void setAddr(Address addr) { this.addr = addr; } public void setFirstName(String firstName) { this.firstName = firstName; } public void setHobbies(List<String> hobbies) { this.hobbies = hobbies; } public void setLastName(String lastName) { this.lastName = lastName; } public void setMailId(String mailId) { this.mailId = mailId; } }
import com.google.gson.*; import java.util.*; public class ObjectToJson { public static void main(String args[]){ Gson gson = new GsonBuilder().setPrettyPrinting().create(); //Gson gson = new Gson(); Employee emp3 = new Employee(); Address addr2 = new Address(); addr2.setCity("Bangalore"); addr2.setCountry("India"); addr2.setPIN("560047"); addr2.setState("Karnataka"); addr2.setStreet("Marthalli"); emp3.setAddr(addr2); emp3.setFirstName("Rama Krishna"); List<String> hobbies3 = new ArrayList<String> (); hobbies3.add("Reading editorial news"); hobbies3.add("climbing hills"); hobbies3.add("chat with friends"); emp3.setHobbies(hobbies3); emp3.setLastName("Gurram"); emp3.setMailId("asdfgh@wxyz.com"); emp3.setId(75); emp3.setDesignation("Tech lead"); String str = gson.toJson(emp3); System.out.println(str); } }
Output
{ "empId": 75, "fName": "Rama Krishna", "lName": "Gurram", "designation": "Tech lead", "mailId": "asdfgh@wxyz.com", "hobbies": [ "Reading editorial news", "climbing hills", "chat with friends" ], "address": { "street": "Marthalli", "city": "Bangalore", "state": "Karnataka", "country": "India", "PIN": "560047" } }
Observe
the output, firstName displayed as fName, lastName as lName, id as empId and
addr as address.
No comments:
Post a Comment