天天看點

Jackson簡單用法

jackson簡單用法

Name類:

[java]  view plain  copy  

Jackson簡單用法
Jackson簡單用法
  1. public class Name {  
  2.     private String firstName;  
  3.     private String lastName;  
  4.     public Name(){}  
  5.     public Name(String firstName, String lastName) {  
  6.         this.firstName = firstName;  
  7.         this.lastName = lastName;  
  8.     }  
  9.     public String getFirstName() {  
  10.         return firstName;  
  11.     }  
  12.     public void setFirstName(String firstName) {  
  13.         this.firstName = firstName;  
  14.     }  
  15.     public String getLastName() {  
  16.         return lastName;  
  17.     }  
  18.     public void setLastName(String lastName) {  
  19.         this.lastName = lastName;  
  20.     }  
  21.     public String toString() {  
  22.         return firstName + " " + lastName;  
  23.     }  
  24. }  

Student類:

[java]  view plain  copy  

Jackson簡單用法
Jackson簡單用法
  1. import java.util.Date;  
  2. public class Student {  
  3.     private int id;  
  4.     private Name name;  
  5.     private String className;  
  6.     private Date birthDay;  
  7.     public Student(){}  
  8.     public Student(int id, Name name, String className, Date birthDay) {  
  9.         super();  
  10.         this.id = id;  
  11.         this.name = name;  
  12.         this.className = className;  
  13.         this.birthDay = birthDay;  
  14.     }  
  15.     public int getId() {  
  16.         return id;  
  17.     }  
  18.     public void setId(int id) {  
  19.         this.id = id;  
  20.     }  
  21.     public Name getName() {  
  22.         return name;  
  23.     }  
  24.     public void setName(Name name) {  
  25.         this.name = name;  
  26.     }  
  27.     public Date getBirthDay() {  
  28.         return birthDay;  
  29.     }  
  30.     public void setBirthDay(Date birthDay) {  
  31.         this.birthDay = birthDay;  
  32.     }  
  33.     public String getClassName() {  
  34.         return className;  
  35.     }  
  36.     public void setClassName(String className) {  
  37.         this.className = className;  
  38.     }  
  39.     @Override  
  40.     public String toString() {  
  41.         return "Student [birthDay=" + birthDay + ", id=" + id + ", name=" + name + ", classname="+ className + "]";  
  42.     }  
  43. }  

測試類

[java]  view plain  copy  

Jackson簡單用法
Jackson簡單用法
  1. import java.text.SimpleDateFormat;  
  2. import java.util.ArrayList;  
  3. import java.util.Date;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7. import org.codehaus.jackson.JsonGenerator;  
  8. import org.codehaus.jackson.map.DeserializationConfig;  
  9. import org.codehaus.jackson.map.ObjectMapper;  
  10. import org.codehaus.jackson.map.SerializationConfig;  
  11. import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;  
  12. import org.codehaus.jackson.type.TypeReference;  
  13. public class JacksonTest {  
  14.     public static ObjectMapper getDefaultObjectMapper() {  
  15.         ObjectMapper mapper = new ObjectMapper();  
  16.         //設定将對象轉換成JSON字元串時候:包含的屬性不能為空或"";    
  17.         //Include.Include.ALWAYS 預設    
  18.         //Include.NON_DEFAULT 屬性為預設值不序列化    
  19.         //Include.NON_EMPTY 屬性為 空("")  或者為 NULL 都不序列化    
  20.         //Include.NON_NULL 屬性為NULL 不序列化    
  21.         mapper.setSerializationInclusion(Inclusion.NON_EMPTY);  
  22.         //設定将MAP轉換為JSON時候隻轉換值不等于NULL的  
  23.         mapper.configure(SerializationConfig.Feature.WRITE_NULL_MAP_VALUES, false);  
  24.         mapper.configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true);  
  25.         mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));  
  26.         //設定有屬性不能映射成PO時不報錯  
  27.         mapper.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);  
  28.         return mapper;  
  29.     }  
  30.     public static void main(String[] args) throws Exception{  
  31.         //準備資料  
  32.         Name name1 = new Name("zhang","san");  
  33.         Name name2 = new Name("li","si");  
  34.         Name name3 = new Name("wang","wu");  
  35.         Student student1 = new Student(1,name1,"一班",new Date());    
  36.         Student student2 = new Student(2,name2,"二班",new Date());    
  37.         Student student3 = new Student(3,name3,"三班",new Date());    
  38.         List<Student> studentList = new ArrayList<Student>();  
  39.         studentList.add(student1);  
  40.         studentList.add(student2);  
  41.         studentList.add(student3);  
  42.         Map<String,Student> studentMap = new HashMap<String, Student>();  
  43.         studentMap.put("1", student1);  
  44.         studentMap.put("2", student2);  
  45.         studentMap.put("3", student3);  
  46.         Student json2object = null;  
  47.         List<Student> json2list = null;  
  48.         Map<String,Student> json2map = null;  
  49.         ObjectMapper mapper = getDefaultObjectMapper();  
  50.         String object4json = mapper.writeValueAsString(student1);  
  51.         System.out.println("Object ----> JSON");  
  52.         System.out.println(object4json);  
  53.         System.out.println("------------------------------------------------------");  
  54.         String listforjson = mapper.writeValueAsString(studentList);  
  55.         System.out.println("List<Object> ----> JSON");  
  56.         System.out.println(listforjson);  
  57.         System.out.println("------------------------------------------------------");  
  58.         String map4json = mapper.writeValueAsString(studentMap);  
  59.         System.out.println("Map<String,Object> ----> JSON");  
  60.         System.out.println(map4json);  
  61.         System.out.println("------------------------------------------------------");  
  62.         json2object = mapper.readValue(object4json, Student.class);  
  63.         System.out.println("JSON ----> Object");  
  64.         System.out.println(json2object);  
  65.         System.out.println("------------------------------------------------------");  
  66.         json2list = mapper.readValue(listforjson, new TypeReference<List<Student>>() {});  
  67.         System.out.println("JSON --> List<Object>");  
  68.         System.out.println(json2list.toString());  
  69.         System.out.println("------------------------------------------------------");  
  70.         json2map = mapper.readValue(map4json, new TypeReference<Map<String,Student>>() {});  
  71.         System.out.println("JSON --> Map<String,Object>");  
  72.         System.out.println(json2map.toString());  
  73.     }  
  74. }