Exams Attended

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Google Gson Interview Questions and Answers

Ques. How to convert Java objects to JSON format?
Ans.
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:

©2021 WithoutBook