Convert object to/from json using Gson
Google GSON is JSON parser and also generator for java, which is widely use to convert object to/from Json. To learn more about Gson one can refer to Gson tutorial. GSON library is used to convert any object to Json and vice-versa. Here in this tutorial we’ll be discussing below popular examples to convert different object to/from Json.
- Convert Object to/from Json.
- Convert List of objects to/from Json.
- Convert Map containing objects.
Simple POJO which will be used as an object in all examples.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
public class Emp { private int id; private String firstName; private String lastName; public Emp() {} public int getId() { return id; } public void setId(int id) { this.id = id; } 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; } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("Id : " + id); sb.append(", FirstName : " + firstName); sb.append(", LastName : " + lastName); return sb.toString(); } } |
1) Convert Object to/from Json
- Example to convert Emp object to Json.
12345678Emp emp = new Emp();emp.setId(1);emp.setFirstName("Rahul");emp.setLastName("Anand");Gson gson = new Gson();String jsonString = gson.toJson(emp);System.out.println(jsonString);Output
1{"id":1,"firstName":"Rahul","lastName":"Anand"} - Example to convert Json to Emp object.
1234String jsonString = "{\"id\":1,\"firstName\":\"Rahul\",\"lastName\":\"Anand\"}";Gson gson = new Gson();Emp emp = gson.fromJson(jsonString, Emp.class);System.out.println(emp);
Output
1Id : 1, FirstName : Rahul, LastName : Anand
2) Convert List of objects to/from Json
- Example to convert List of Emp objects to Json.
1234567891011121314151617List<Emp> employees = new ArrayList<Emp>();Emp emp1 = new Emp();emp1.setId(1);emp1.setFirstName("Rahul");emp1.setLastName("Anand");Emp emp2 = new Emp();emp2.setId(2);emp2.setFirstName("Daniel");emp2.setLastName("Anderson");employees.add(emp1);employees.add(emp2);Gson gson = new Gson();String jsonString = gson.toJson(employees);System.out.println(jsonString);
Output
1[{"id":1,"firstName":"Rahul","lastName":"Anand"},{"id":2,"firstName":"Daniel","lastName":"Anderson"}] - Example to convert Json to List of Emp objects.
1234String jsonString = "[{\"id\":1,\"firstName\":\"Rahul\",\"lastName\":\"Anand\"},{\"id\":2,\"firstName\":\"Daniel\",\"lastName\":\"Anderson\"}]";Gson gson = new Gson();List<Emp> employees = gson.fromJson(jsonString, new TypeToken<List<Emp>>(){}.getType());System.out.println(employees);
Json object in user readable format.
123456789101112[{"id": 1,"firstName": "Rahul","lastName": "Anand"},{"id": 2,"firstName": "Daniel","lastName": "Anderson"}]Output
1[Id : 1, FirstName : Rahul, LastName : Anand, Id : 2, FirstName : Daniel, LastName : Anderson]
3) Convert Map containing objects.
- Example to convert Map containing Emp objects to Json.
1234567891011121314151617Map<Integer, Emp> employees = new HashMap<Integer, Emp>();Emp emp1 = new Emp();emp1.setId(1);emp1.setFirstName("Rahul");emp1.setLastName("Anand");Emp emp2 = new Emp();emp2.setId(2);emp2.setFirstName("Daniel");emp2.setLastName("Anderson");employees.put(1, emp1);employees.put(2, emp2);Gson gson = new Gson();String jsonString = gson.toJson(employees);System.out.println(jsonString);
Output
1{"1":{"id":1,"firstName":"Rahul","lastName":"Anand"},"2":{"id":2,"firstName":"Daniel","lastName":"Anderson"}} - Example to convert Json to Map.
12345String jsonString = "{\"1\":{\"id\":1,\"firstName\":\"Rahul\",\"lastName\":\"Anand\"},\"2\":{\"id\":2,\"firstName\":\"Daniel\",\"lastName\":\"Anderson\"}}";Gson gson = new Gson();Type type = new TypeToken<Map<Integer, Emp>>(){}.getType();Map<Integer, Emp> employees = gson.fromJson(jsonString, type);System.out.println(employees);
Json String in user readable format
123456789101112{"1": {"id": 1,"firstName": "Rahul","lastName": "Anand"},"2": {"id": 2,"firstName": "Daniel","lastName": "Anderson"}}Output
1{1=Id : 1, FirstName : Rahul, Last Name : Anand, 2=Id : 2, FirstName : Daniel, Last Name : Anderson}
Stay tuned for more tutorials and examples.