Sep 14, 2018

How To Convert Java Object To / From JSON

Use Gson from Google is very convenient.

//To JSON:
DataObject obj = new DataObject();
Gson gson = new Gson();
// convert java object to JSON format,
// and returned as JSON formatted string 
String json = gson.toJson(obj);        
//From JSON:
//convert the json string back to object
DataObject obj = gson.fromJson(br, DataObject.class);
Gson Maven Dependency:
<dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.2.4</version>
            <type>jar</type>
</dependency>