Jackson简单用法

jackson简单用法

Name类:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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
  在CODE上查看代码片 派生到我的代码片
  1. import java.util.Date;  
  2.   
  3. public class Student {  
  4.     private int id;  
  5.     private Name name;  
  6.     private String className;  
  7.     private Date birthDay;  
  8.     public Student(){}  
  9.     public Student(int id, Name name, String className, Date birthDay) {  
  10.         super();  
  11.         this.id = id;  
  12.         this.name = name;  
  13.         this.className = className;  
  14.         this.birthDay = birthDay;  
  15.     }  
  16.     public int getId() {  
  17.         return id;  
  18.     }  
  19.     public void setId(int id) {  
  20.         this.id = id;  
  21.     }  
  22.     public Name getName() {  
  23.         return name;  
  24.     }  
  25.     public void setName(Name name) {  
  26.         this.name = name;  
  27.     }  
  28.     public Date getBirthDay() {  
  29.         return birthDay;  
  30.     }  
  31.     public void setBirthDay(Date birthDay) {  
  32.         this.birthDay = birthDay;  
  33.     }  
  34.     public String getClassName() {  
  35.         return className;  
  36.     }  
  37.     public void setClassName(String className) {  
  38.         this.className = className;  
  39.     }  
  40.     @Override  
  41.     public String toString() {  
  42.         return "Student [birthDay=" + birthDay + ", id=" + id + ", name=" + name + ", classname="+ className + "]";  
  43.     }  
  44. }  

测试类

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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.   
  8. import org.codehaus.jackson.JsonGenerator;  
  9. import org.codehaus.jackson.map.DeserializationConfig;  
  10. import org.codehaus.jackson.map.ObjectMapper;  
  11. import org.codehaus.jackson.map.SerializationConfig;  
  12. import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;  
  13. import org.codehaus.jackson.type.TypeReference;  
  14.   
  15. public class JacksonTest {  
  16.     public static ObjectMapper getDefaultObjectMapper() {  
  17.         ObjectMapper mapper = new ObjectMapper();  
  18.         //设置将对象转换成JSON字符串时候:包含的属性不能为空或"";    
  19.         //Include.Include.ALWAYS 默认    
  20.         //Include.NON_DEFAULT 属性为默认值不序列化    
  21.         //Include.NON_EMPTY 属性为 空("")  或者为 NULL 都不序列化    
  22.         //Include.NON_NULL 属性为NULL 不序列化    
  23.         mapper.setSerializationInclusion(Inclusion.NON_EMPTY);  
  24.         //设置将MAP转换为JSON时候只转换值不等于NULL的  
  25.         mapper.configure(SerializationConfig.Feature.WRITE_NULL_MAP_VALUES, false);  
  26.         mapper.configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true);  
  27.         mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));  
  28.         //设置有属性不能映射成PO时不报错  
  29.         mapper.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);  
  30.         return mapper;  
  31.     }  
  32.     public static void main(String[] args) throws Exception{  
  33.         //准备数据  
  34.         Name name1 = new Name("zhang","san");  
  35.         Name name2 = new Name("li","si");  
  36.         Name name3 = new Name("wang","wu");  
  37.         Student student1 = new Student(1,name1,"一班",new Date());    
  38.         Student student2 = new Student(2,name2,"二班",new Date());    
  39.         Student student3 = new Student(3,name3,"三班",new Date());    
  40.         List<Student> studentList = new ArrayList<Student>();  
  41.         studentList.add(student1);  
  42.         studentList.add(student2);  
  43.         studentList.add(student3);  
  44.         Map<String,Student> studentMap = new HashMap<String, Student>();  
  45.         studentMap.put("1", student1);  
  46.         studentMap.put("2", student2);  
  47.         studentMap.put("3", student3);  
  48.         Student json2object = null;  
  49.         List<Student> json2list = null;  
  50.         Map<String,Student> json2map = null;  
  51.         ObjectMapper mapper = getDefaultObjectMapper();  
  52.           
  53.         /* Object --> JSON */  
  54.         String object4json = mapper.writeValueAsString(student1);  
  55.         System.out.println("Object ----> JSON");  
  56.         System.out.println(object4json);  
  57.         System.out.println("------------------------------------------------------");  
  58.           
  59.         /* List<Object> --> JSON */  
  60.         String listforjson = mapper.writeValueAsString(studentList);  
  61.         System.out.println("List<Object> ----> JSON");  
  62.         System.out.println(listforjson);  
  63.         System.out.println("------------------------------------------------------");  
  64.           
  65.         /* Map<String,Object> ----> JSON */  
  66.         String map4json = mapper.writeValueAsString(studentMap);  
  67.         System.out.println("Map<String,Object> ----> JSON");  
  68.         System.out.println(map4json);  
  69.         System.out.println("------------------------------------------------------");  
  70.           
  71.         /* JSON --> Object */  
  72.         json2object = mapper.readValue(object4json, Student.class);  
  73.         System.out.println("JSON ----> Object");  
  74.         System.out.println(json2object);  
  75.         System.out.println("------------------------------------------------------");  
  76.         /* JSON --> List<Object> */  
  77.         json2list = mapper.readValue(listforjson, new TypeReference<List<Student>>() {});  
  78.         System.out.println("JSON --> List<Object>");  
  79.         System.out.println(json2list.toString());  
  80.         System.out.println("------------------------------------------------------");  
  81.         /* JSON --> Map<String,Object> */  
  82.         json2map = mapper.readValue(map4json, new TypeReference<Map<String,Student>>() {});  
  83.         System.out.println("JSON --> Map<String,Object>");  
  84.         System.out.println(json2map.toString());  
  85.     }  
  86. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值