jackson簡單用法
Name類:
[java] view plain copy
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5Gcu82Yp9VRE90Qvw1c0V2czF2LcRXZu5ibkN3YuUGZvN2Lc9CX6MHc0RHaiojIsJye.png)
- public class Name {
- private String firstName;
- private String lastName;
- public Name(){}
- public Name(String firstName, String lastName) {
- this.firstName = firstName;
- this.lastName = lastName;
- }
- public String getFirstName() {
- return firstName;
- }
- public void setFirstName(String firstName) {
- this.firstName = firstName;
- }
- public String getLastName() {
- return lastName;
- }
- public void setLastName(String lastName) {
- this.lastName = lastName;
- }
- public String toString() {
- return firstName + " " + lastName;
- }
- }
Student類:
[java] view plain copy
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5Gcu82Yp9VRE90Qvw1c0V2czF2LcRXZu5ibkN3YuUGZvN2Lc9CX6MHc0RHaiojIsJye.png)
- import java.util.Date;
- public class Student {
- private int id;
- private Name name;
- private String className;
- private Date birthDay;
- public Student(){}
- public Student(int id, Name name, String className, Date birthDay) {
- super();
- this.id = id;
- this.name = name;
- this.className = className;
- this.birthDay = birthDay;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public Name getName() {
- return name;
- }
- public void setName(Name name) {
- this.name = name;
- }
- public Date getBirthDay() {
- return birthDay;
- }
- public void setBirthDay(Date birthDay) {
- this.birthDay = birthDay;
- }
- public String getClassName() {
- return className;
- }
- public void setClassName(String className) {
- this.className = className;
- }
- @Override
- public String toString() {
- return "Student [birthDay=" + birthDay + ", id=" + id + ", name=" + name + ", classname="+ className + "]";
- }
- }
測試類
[java] view plain copy
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5Gcu82Yp9VRE90Qvw1c0V2czF2LcRXZu5ibkN3YuUGZvN2Lc9CX6MHc0RHaiojIsJye.png)
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import org.codehaus.jackson.JsonGenerator;
- import org.codehaus.jackson.map.DeserializationConfig;
- import org.codehaus.jackson.map.ObjectMapper;
- import org.codehaus.jackson.map.SerializationConfig;
- import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
- import org.codehaus.jackson.type.TypeReference;
- public class JacksonTest {
- public static ObjectMapper getDefaultObjectMapper() {
- ObjectMapper mapper = new ObjectMapper();
- //設定将對象轉換成JSON字元串時候:包含的屬性不能為空或"";
- //Include.Include.ALWAYS 預設
- //Include.NON_DEFAULT 屬性為預設值不序列化
- //Include.NON_EMPTY 屬性為 空("") 或者為 NULL 都不序列化
- //Include.NON_NULL 屬性為NULL 不序列化
- mapper.setSerializationInclusion(Inclusion.NON_EMPTY);
- //設定将MAP轉換為JSON時候隻轉換值不等于NULL的
- mapper.configure(SerializationConfig.Feature.WRITE_NULL_MAP_VALUES, false);
- mapper.configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true);
- mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
- //設定有屬性不能映射成PO時不報錯
- mapper.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);
- return mapper;
- }
- public static void main(String[] args) throws Exception{
- //準備資料
- Name name1 = new Name("zhang","san");
- Name name2 = new Name("li","si");
- Name name3 = new Name("wang","wu");
- Student student1 = new Student(1,name1,"一班",new Date());
- Student student2 = new Student(2,name2,"二班",new Date());
- Student student3 = new Student(3,name3,"三班",new Date());
- List<Student> studentList = new ArrayList<Student>();
- studentList.add(student1);
- studentList.add(student2);
- studentList.add(student3);
- Map<String,Student> studentMap = new HashMap<String, Student>();
- studentMap.put("1", student1);
- studentMap.put("2", student2);
- studentMap.put("3", student3);
- Student json2object = null;
- List<Student> json2list = null;
- Map<String,Student> json2map = null;
- ObjectMapper mapper = getDefaultObjectMapper();
- String object4json = mapper.writeValueAsString(student1);
- System.out.println("Object ----> JSON");
- System.out.println(object4json);
- System.out.println("------------------------------------------------------");
- String listforjson = mapper.writeValueAsString(studentList);
- System.out.println("List<Object> ----> JSON");
- System.out.println(listforjson);
- System.out.println("------------------------------------------------------");
- String map4json = mapper.writeValueAsString(studentMap);
- System.out.println("Map<String,Object> ----> JSON");
- System.out.println(map4json);
- System.out.println("------------------------------------------------------");
- json2object = mapper.readValue(object4json, Student.class);
- System.out.println("JSON ----> Object");
- System.out.println(json2object);
- System.out.println("------------------------------------------------------");
- json2list = mapper.readValue(listforjson, new TypeReference<List<Student>>() {});
- System.out.println("JSON --> List<Object>");
- System.out.println(json2list.toString());
- System.out.println("------------------------------------------------------");
- json2map = mapper.readValue(map4json, new TypeReference<Map<String,Student>>() {});
- System.out.println("JSON --> Map<String,Object>");
- System.out.println(json2map.toString());
- }
- }