Skip to Content
RESTful Java Web Services - Third Edition
book

RESTful Java Web Services - Third Edition

by Balachandar Bogunuva Mohanram
November 2017
Intermediate to advanced
420 pages
10h 29m
English
Packt Publishing
Content preview from RESTful Java Web Services - Third Edition

Generating the JSON representation from the object model

Gson has simplified APIs to convert the object model into the JSON content. Depending upon the type of the object model, you can use either of the following APIs to get the JSON representation:

  • To convert a Java object into JSON, you can call the toJson(Object src) method. Here is an example:
// Get Employee object that needs  //to be converted into JSON Employee emp=getEmployee(); Gson gson = new Gson(); // create JSON String from Object String jsonEmp = gson.toJson(emp); 
  • To convert a parameterized collection into a JSON string, you can use toJson(Object src, Type typeOfSrc). Here is an example:
//Get Employee list that needs to be converted into JSON List<Employee> employees= ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

RESTful Java Web Services, Second Edition

RESTful Java Web Services, Second Edition

Jobinesh Purushothaman
Java Web Services

Java Web Services

David A Chappell, Tyler Jewell

Publisher Resources

ISBN: 9781788294041Supplemental Content