We will see a simple example about converting objects to/from JSON objects in Java by Jackson. First, add maven dependency for jackson.
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.4</version>
</dependency>
And two typical Person and Address classes.
Person.java
public class Person {
private String name;
private String lastName;
private int age;
private List<Address> addressList;
@JsonCreator
public Person(@JsonProperty("name") String name,
@JsonProperty("lastName") String lastName,
@JsonProperty("age") int age,
@JsonProperty("addressList") List<Address> addressList) {
this.name = name;
this.lastName = lastName;
this.age = age;
this.addressList = addressList;
}
// getters and setters
@Override
public String toString(){
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("name: ")
.append(this.name).append("\n")
.append("lastName: ")
.append(this.lastName).append("\n")
.append("age: ")
.append(this.age).append("\n");
for (Address address: this.addressList) {
stringBuilder.append(address.toString());
}
return stringBuilder.toString();
}
}
Address.java
public class Address {
private int zipcode;
private String street;
@JsonCreator
public Address(@JsonProperty("zipcode") int zipcode,
@JsonProperty("street") String street) {
this.zipcode = zipcode;
this.street = street;
}
// getters and setters
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("zipcode: ")
.append(this.zipcode).append("\n")
.append("street: ")
.append(this.street).append("\n");
return stringBuilder.toString();
}
}
Notes
-
@JsonCreator
annotation is used for constructors or static factory methods to construct instances from Json. This is called property base creators. Property base creators can have one more or more parameters which all of them have to annotated by@JsonProperty
annotations. -
@JsonProperty
annotation is used to bind data by a given name.
1.Object to JSON
public class JacksonExample {
public static void main(String[] args) throws JsonProcessingException {
Address homeAddress = new Address(12345, "Stenhammer Drive");
Address workAddress = new Address(7986, "Market Street");
List<Address> addressList = new ArrayList<>();
addressList.add(homeAddress);
addressList.add(workAddress);
Person person = new Person("Sawyer", "Bootstrapper", 23, addressList);
// object mapper is the main object to marshall (write) data into JSON
ObjectMapper objectMapper = new ObjectMapper();
String value = objectMapper.writeValueAsString(person);
System.out.println(value);
}
}
Output will be;
{
"name":"Sawyer",
"lastName":"Bootstrapper",
"age":23,
"addressList":[
{
"zipcode":12345,
"street":"Stenhammer Drive"
},
{
"zipcode":7986,
"street":"Market Street"
}
]
}
2.JSON to Object
public class JacksonExample {
private static String jsonValue = "{\"name\":\"Sawyer\",\"lastName\":\"Bootstrapper\",\"age\":23,\"addressList\":[{\"zipcode\":12345,\"street\":" +
"\"Stenhammer Drive\"},{\"zipcode\":7986,\"street\":\"Market Street\"}]}";
public static void main(String[] args) throws IOException {
Person personValue = objectMapper.readValue(jsonValue, Person.class);
System.out.println(personValue.toString());
}
}
Output will be;
name: Sawyer lastName: Bootstrapper age: 23 zipcode: 12345 street: Stenhammer Drive zipcode: 7986 street: Market Street
Comments powered by Disqus.