Google Gson Interview Questions and Answers
Question: How to convert Java objects to JSON format?Answer:To convert the java objects to JSON format, use toJson() method. Employee employee = new Employee(); employee.setId(1); employee.setFirstName("Arindam"); employee.setLastName("Ghosh"); employee.setRoles(Arrays.asList("FINANCE", "MANAGER")); Gson gson = new Gson(); System.out.println(gson.toJson(employee)); Output: { "id":1, "firstName":"Rob", "lastName":"Bosch", "roles":["FIINANCE","MANAGER"] } |
Is it helpful?
Yes
No
Most helpful rated by users:
- What is Google-Gson?
- How to convert Java objects to JSON format?
- What are the two ways to create Gson objects?
- What is Instance Creator? Why and when do we require this?
- How to custom Serialization and De-serialization in Gson?