Jackson 2 Convert Java Object to / from JSON
By:Roy.LiuLast updated:2019-08-17
In this tutorial, we will show you how to use Jackson 2.x to convert Java objects to / from a JSON.
1. Basic
1.1 Convert a Staff object to from JSON.
writeValue(...) – Java Objects to JSON
ObjectMapper mapper = new ObjectMapper(); // Java object to JSON file mapper.writeValue(new File("c:\\test\\staff.json"), new Staff()); // Java object to JSON string String jsonString = mapper.writeValueAsString(object);
readValue(...) – JSON to Java Objects
ObjectMapper mapper = new ObjectMapper(); //JSON file to Java object Staff obj = mapper.readValue(new File("c:\\test\\staff.json"), Staff.class); //JSON URL to Java object Staff obj = mapper.readValue(new URL("http://some-domains/api/name.json"), Staff.class); //JSON string to Java Object Staff obj = mapper.readValue("{'name' : 'mkyong'}", Staff.class);
P.S Tested with Jackson 2.9.8
Note
Read this How to parse JSON with Jackson, containing Jackson examples like Object to/from JSON, @JsonView, @JsonProperty, @JsonInclude, @JsonIgnore, and some FAQs.
Read this How to parse JSON with Jackson, containing Jackson examples like Object to/from JSON, @JsonView, @JsonProperty, @JsonInclude, @JsonIgnore, and some FAQs.
1. Download Jackson
1.1 Declares jackson-databind, it will pull in jackson-annotations and jackson-core
pom.xml
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.8</version> </dependency>
1.2 Review the Jackson dependencies :
Terminal
$ mvn dependency:tree \- com.fasterxml.jackson.core:jackson-databind:jar:2.9.8:compile [INFO] +- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile [INFO] \- com.fasterxml.jackson.core:jackson-core:jar:2.9.8:compile
Difference between Jackson 1 and Jackson 2
Most of the APIs still maintains the same method name and signature, just the packaging is different.
Most of the APIs still maintains the same method name and signature, just the packaging is different.
- Jackson 1.x – org.codehaus.jackson.map
- Jackson 2.x – com.fasterxml.jackson.databind
2. POJO
A simple Java object for testing.
Staff.java
public class Staff { private String name; private int age; private String[] position; // Array private List<String> skills; // List private Map<String, BigDecimal> salary; // Map // getters , setters, some boring stuff
3. Java Objects to JSON
JacksonExample1.java
package com.mkyong; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.File; import java.io.IOException; import java.math.BigDecimal; import java.util.Arrays; import java.util.HashMap; import java.util.Map; public class JacksonExample1 { public static void main(String[] args) { ObjectMapper mapper = new ObjectMapper(); Staff staff = createStaff(); try { // Java objects to JSON file mapper.writeValue(new File("c:\\test\\staff.json"), staff); // Java objects to JSON string - compact-print String jsonString = mapper.writeValueAsString(staff); System.out.println(jsonString); // Java objects to JSON string - pretty-print String jsonInString2 = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(staff); System.out.println(jsonInString2); } catch (IOException e) { e.printStackTrace(); private static Staff createStaff() { Staff staff = new Staff(); staff.setName("mkyong"); staff.setAge(38); staff.setPosition(new String[]{"Founder", "CTO", "Writer"}); Map<String, BigDecimal> salary = new HashMap() {{ put("2010", new BigDecimal(10000)); put("2012", new BigDecimal(12000)); put("2018", new BigDecimal(14000)); }}; staff.setSalary(salary); staff.setSkills(Arrays.asList("java", "python", "node", "kotlin")); return staff;
Output
c:\\test\\staff.json
{"name":"mkyong","age":38,"position":["Founder","CTO","Writer"],"skills":["java","python","node","kotlin"],"salary":{"2018":14000,"2012":12000,"2010":10000}}
Terminal
{"name":"mkyong","age":38,"position":["Founder","CTO","Writer"],"skills":["java","python","node","kotlin"],"salary":{"2018":14000,"2012":12000,"2010":10000}} "name" : "mkyong", "age" : 38, "position" : [ "Founder", "CTO", "Writer" ], "skills" : [ "java", "python", "node", "kotlin" ], "salary" : { "2018" : 14000, "2012" : 12000, "2010" : 10000
4. JSON to Java Object
JacksonExample2.java
package com.mkyong; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.File; import java.io.IOException; public class JacksonExample2 { public static void main(String[] args) { ObjectMapper mapper = new ObjectMapper(); try { // JSON file to Java object Staff staff = mapper.readValue(new File("c:\\test\\staff.json"), Staff.class); // JSON string to Java object String jsonInString = "{\"name\":\"mkyong\",\"age\":37,\"skills\":[\"java\",\"python\"]}"; Staff staff2 = mapper.readValue(jsonInString, Staff.class); // compact print System.out.println(staff2); // pretty print String prettyStaff1 = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(staff2); System.out.println(prettyStaff1); } catch (IOException e) { e.printStackTrace();
Output
Staff{name='mkyong', age=37, position=null, skills=[java, python], salary=null} "name" : "mkyong", "age" : 37, "position" : null, "skills" : [ "java", "python" ], "salary" : null
Note
More Jackson examples read this – How to parse JSON with Jackson
More Jackson examples read this – How to parse JSON with Jackson
From:一号门
COMMENTS