The JSON Object Mapper will hydrate an entity from a JSON string.
<repositories>
<repository>
<id>scottbyrns-snapshots</id>
<url>https://github.com/scottbyrns/Scottbyrns-Maven-Repo/raw/master/snapshots</url>
</repository>
</repositories>
<dependency>
<groupId>com.scottbyrns.utilities</groupId>
<artifactId>json-object-mapper</artifactId>
<version>1.1.3-SNAPSHOT</version>
</dependency>
/**
* Example entity for testing the JSON object mapper.
*/
public class GeoLocation {
private double latitude;
private double longitude;
public void setLatitude (double latitude) {
this.latitude = latitude;
}
public double getLatitude () {
return latitude;
}
public void setLongitude (double longitude) {
this.longitude = longitude;
}
public double getLongitude () {
return longitude;
}
}
GeoLocation location = JSONObjectMapper.mapJSONStringToEntity(
"{ \"latitude\": 46.0231, \"longitude\": -116.1239 }",
GeoLocation.class
);
Note: The properties of your entity class must match the JSON keys.
The JSONObjectMapper can throw one of two exceptions:
catch (InvalidJSONStringException e) {
// The JSON string was invalid.
}
catch (FatalMappingException e) {
// The entity was not compatible.
}
Having trouble with the JSON Object Mapper? Generate the documentation with JavaDoc or contact me, (@scottbyrns) directly and I'll help you sort it out.