Most of the marshalling I have to do it done by Spring. However, I sometimes have to do this manually. Let’s see how easy it is with Jackson 2 (FasterXML).
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.4.3</version> </dependency>
The ObjectMapper is the object that you will instantiate and use for all conversions.
Given an object, it is pretty easy to use an ObjectMapper.
ObjectMapper objectMapper = new ObjectMapper(); String myString = objectMapper.writeValueAsString(myObject);
If you have a JSON string and need to convert that to an object, you also use the ObjectMapper.
ObjectMapper objectMapper = new ObjectMapper(); MyObject myObject = objectMapper.readValue(myJsonString, MyObject.class);
sheltonn May 12th, 2016