How To Convert Java Object To / From JSON
Use Gson from Google is very convenient.
//To JSON:Gson Maven Dependency:
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);
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
<type>jar</type>
</dependency>
Comments
Post a Comment