Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Google%20Gson%20Interview%20Questions%20and%20Answers

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:

©2024 WithoutBook