1, Download Jackson Tool Package (jackson-core-2.2.3.jar jackson-annotations-2.2.3.jar jackson-databind-2.2.3.jar)
jackson-core-2.2.3.jar core package
http://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-core/2.2.3/jackson-core-2.2.3.jar
jackson-annotations-2.2.3.jar provides Json annotation support
http://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-annotations/2.2.3/jackson-annotations-2.2.3.jar
jackson-databind-2.2.3.jar
http://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-databind/2.2.3/jackson-databind-2.2.3.jar
2. User class used for JSON serialization and deserialization
package com.st.json; import java.util.Date; /** * @Description: User class used for JSON serialization and deserialization * @author Mr.Li *@date April 21, 2018 at 10:55:34 PM */ public class User { /** * JSON annotation Jackson provides a series of annotations to facilitate the control of JSON serialization and deserialization. Some commonly used annotations are described below. * * @JsonIgnore This annotation is used on properties to ignore this property when performing JSON operations. * @JsonFormat This annotation is used on properties to directly convert the Date type to the desired format, such as @JsonFormat(pattern = "yyyy-MM-dd * HH-mm-ss")。 * @JsonProperty This annotation is used on properties to serialize the name of the property to another name, such as serializing the trueName property to name, @JsonProperty("name"). */ private Integer id; private String name; private Integer age; private Date birthday; private String email; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", age=" + age + ", birthday=" + birthday + ", email=" + email + "]"; } public User(Integer id, String name, Integer age, Date birthday, String email) { super(); this.id = id; this.name = name; this.age = age; this.birthday = birthday; this.email = email; } public User() { super(); // TODO Auto-generated constructor stub } }
3. The use of Jackson serialization and deserialization
package com.st.json; import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; /** * @Description: Jackson's use of JSON [serialize] and [deserialize] * @author Mr.Li *@date April 21, 2018 11:05:31 PM */ public class JacksonDemo { public static void main(String[] args) throws ParseException, IOException { jsonTest(); } /** * Use of jackson serialization * @throws ParseException * @throws JsonProcessingException */ public static void jackTest() throws ParseException, JsonProcessingException { User u = new User(); u.setId ( 1 ); u.setName("curry"); u.setAge(30); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); u.setBirthday(dateFormat.parse("1988-9-21")); u.setEmail("[email protected]"); /** * ObjectMapper is the core of JSON operations, all JSON operations of Jackson are implemented in ObjectMapper. * ObjectMapper has multiple JSON serialization methods, which can save JSON strings in different media such as File and OutputStream. * writeValue(File arg0, Object arg1) convert arg1 to json sequence and save it to arg0 file. * writeValue(OutputStream arg0, Object arg1) converts arg1 into a json sequence and saves it to the arg0 output stream. * writeValueAsBytes(Object arg0) converts arg0 into a json sequence and outputs the result as a byte array. * writeValueAsString(Object arg0) converts arg0 into a json sequence and outputs the result as a string. */ ObjectMapper mapper = new ObjectMapper(); //User对象转Json, //输出{"id":1,"name":"curry","age":30,"birthday":590774400000,"email":"[email protected]"} String jsonValue = mapper.writeValueAsString(u); System.out.println(jsonValue); User u2 = new User(); u2.setId( 2 ); u2.setName("KD"); u2.setAge(29); u2.setBirthday(dateFormat.parse("1989-9-21")); u2.setEmail("[email protected]"); List<User> users = new ArrayList<>(); users.add(u); users.add(u2); String jsonList = mapper.writeValueAsString(users); System.out.println(jsonList); } /** * JSON to Java object [JSON deserialization] * @throws IOException * @throws JsonMappingException * @throws JsonParseException */ public static void jsonTest() throws JsonParseException, JsonMappingException, IOException { String json = " {\"id\":3, \"name\":\"小明\", \"age\":18, \"birthday\":590774400000, \"email\":\"[email protected]\"} "; /** * ObjectMapper supports JSON deserialization from byte[], File, InputStream, String, etc. data. */ ObjectMapper mapper = new ObjectMapper(); User user = mapper.readValue(json, User.class); System.out.println(user); } }