jackson json 简单入门

来源:互联网 发布:同性约会软件 编辑:程序博客网 时间:2024/05/17 08:04

本文转自:http://blog.csdn.net/nomousewch/article/details/8949899

  • 概述

         Jackson库(http://jackson.codehaus.org),是基于java语言的开源json格式解析工具,整个库(使用最新的2.2版本)包含3个jar包:

  1. jackson-core.jar——核心包(必须),提供基于“流模式”解析的API。
  2. jackson-databind——数据绑定包(可选),提供基于“对象绑定”和“树模型”相关API。
  3. jackson-annotations——注解包(可选),提供注解功能。


  • Jackson的优势

         相对于java json解析的其他库,诸如json-lib、gson包,Jackson具有以下优点:

  1. 功能全面,提供多种模式的json解析方式,“对象绑定”使用方便,利用注解包能为我们开发提供很多便利。
  2. 性能较高,“流模式”的解析效率超过绝大多数类似的json包。
  • 重要API
  1. 核心包:JsonPaser(json流读取),JsonGenerator(json流输出)。
  2. 数据绑定包:ObjectMapper(构建树模式和对象绑定模式),JsonNode(树节点)。
  • 简单例子

         在实际应用中,用的最多的是数据对象绑定的模式,即将一个对象序列化为json字符串和将一串json字符串反序列化为java对象或Map。

         Person类:

[java] view plaincopy
  1. public class Person {  
  2.     private String name;  
  3.     private int age;  
  4.   
  5.     public String getName() {  
  6.         return name;  
  7.     }  
  8.   
  9.     public void setName(String name) {  
  10.         this.name = name;  
  11.     }  
  12.   
  13.     public int getAge() {  
  14.         return age;  
  15.     }  
  16.   
  17.     public void setAge(int age) {  
  18.         this.age = age;  
  19.     }  
  20.   
  21.     public Person(String name, int age) {  
  22.         this.name = name;  
  23.         this.age = age;  
  24.     }  
  25.   
  26.     public Person() {  
  27.     }  
  28.   
  29. }  
          测试类:

[java] view plaincopy
  1. public class Demo {  
  2.     public static void main(String[] args) {  
  3.   
  4.         // writeJsonObject();  
  5.   
  6.         // readJsonObject();  
  7.   
  8.         // readJsonMap();  
  9.     }  
  10.   
  11.       
  12.   
  13.     // 直接写入一个对象  
  14.     public static void writeJsonObject() {  
  15.         ObjectMapper mapper = new ObjectMapper();  
  16.         Person person = new Person("nomouse"25);  
  17.         try {  
  18.             mapper.writeValue(new File("c:/person.json"), person);  
  19.         } catch (JsonGenerationException e) {  
  20.             e.printStackTrace();  
  21.         } catch (JsonMappingException e) {  
  22.             e.printStackTrace();  
  23.         } catch (IOException e) {  
  24.             e.printStackTrace();  
  25.         }  
  26.     }  
  27.   
  28.     // 直接将一个json转化为对象  
  29.     public static void readJsonObject() {  
  30.         ObjectMapper mapper = new ObjectMapper();  
  31.   
  32.         try {  
  33.             Person person = mapper.readValue(new File("c:/person.json"),  
  34.                     Person.class);  
  35.             System.out.println(person.getName() + ":" + person.getAge());  
  36.         } catch (JsonParseException e) {  
  37.             e.printStackTrace();  
  38.         } catch (JsonMappingException e) {  
  39.             e.printStackTrace();  
  40.         } catch (IOException e) {  
  41.             e.printStackTrace();  
  42.         }  
  43.     }  
  44.   
  45.     // 直接转化为map  
  46.     public static void readJsonMap() {  
  47.         ObjectMapper mapper = new ObjectMapper();  
  48.   
  49.         try {  
  50.             // 需要注意的是这里的Map实际为一个LikedHashMap,即链式哈希表,可以按照读入顺序遍历  
  51.             Map map = mapper.readValue(new File("c:/person.json"), Map.class);  
  52.             System.out.println(map.get("name") + ":" + map.get("age"));  
  53.         } catch (JsonParseException e) {  
  54.             e.printStackTrace();  
  55.         } catch (JsonMappingException e) {  
  56.             e.printStackTrace();  
  57.         } catch (IOException e) {  
  58.             e.printStackTrace();  
  59.         }  
  60.     }  
  61.   
  62. }  

下面介绍一下最常用的一些注解

  • @JsonIgnoreProperties

         此注解是类注解,作用是json序列化时将java bean中的一些属性忽略掉,序列化和反序列化都受影响。

  • @JsonIgnore

         此注解用于属性或者方法上(最好是属性上),作用和上面的@JsonIgnoreProperties一样。

  • @JsonFormat

        此注解用于属性或者方法上(最好是属性上),可以方便的把Date类型直接转化为我们想要的模式,比如@JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss")

  • @JsonSerialize

        此注解用于属性或者getter方法上(本人jackson-all-1.7.6.jar测试,在getter方法上使用生效),用于在序列化时嵌入我们自定义的代码,比如序列化一个double时在其后面限制两位小数点。

[java] view plaincopy
  1. public class CustomDoubleSerialize extends JsonSerializer<Double> {  
  2.   
  3.     private DecimalFormat df = new DecimalFormat("##.00");  
  4.   
  5.     @Override  
  6.     public void serialize(Double value, JsonGenerator jgen,  
  7.             SerializerProvider provider) throws IOException,  
  8.             JsonProcessingException {  
  9.   
  10.         jgen.writeString(df.format(value));  
  11.     }  
  12. }  

  • @JsonDeserialize

         此注解用于属性或者setter方法上(本人jackson-all-1.7.6.jar测试,在setter方法上使用生效),用于在反序列化时可以嵌入我们自定义的代码,类似于上面的@JsonSerialize

[java] view plaincopy
  1. public class CustomDateDeserialize extends JsonDeserializer<Date> {  
  2.   
  3.     private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
  4.   
  5.     @Override  
  6.     public Date deserialize(JsonParser jp, DeserializationContext ctxt)  
  7.             throws IOException, JsonProcessingException {  
  8.   
  9.         Date date = null;  
  10.         try {  
  11.             date = sdf.parse(jp.getText());  
  12.         } catch (ParseException e) {  
  13.             e.printStackTrace();  
  14.         }  
  15.         return date;  
  16.     }  
  17. }  

  • 完整例子

       

[java] view plaincopy
  1. //表示序列化时忽略的属性  
  2. @JsonIgnoreProperties(value = { "word" })  
  3. public class Person {  
  4.     private String name;  
  5.     private int age;  
  6.     private boolean sex;  
  7.     private Date birthday;  
  8.     private String word;  
  9.     private double salary;  
  10.   
  11.     public String getName() {  
  12.         return name;  
  13.     }  
  14.   
  15.     public void setName(String name) {  
  16.         this.name = name;  
  17.     }  
  18.   
  19.     public int getAge() {  
  20.         return age;  
  21.     }  
  22.   
  23.     public void setAge(int age) {  
  24.         this.age = age;  
  25.     }  
  26.   
  27.     public boolean isSex() {  
  28.         return sex;  
  29.     }  
  30.   
  31.     public void setSex(boolean sex) {  
  32.         this.sex = sex;  
  33.     }  
  34.   
  35.     public Date getBirthday() {  
  36.         return birthday;  
  37.     }  
  38.   
  39.     // 反序列化一个固定格式的Date  
  40.     @JsonDeserialize(using = CustomDateDeserialize.class)  
  41.     public void setBirthday(Date birthday) {  
  42.         this.birthday = birthday;  
  43.     }  
  44.   
  45.     public String getWord() {  
  46.         return word;  
  47.     }  
  48.   
  49.     public void setWord(String word) {  
  50.         this.word = word;  
  51.     }  
  52.   
  53.     // 序列化指定格式的double格式  
  54.     @JsonSerialize(using = CustomDoubleSerialize.class)  
  55.     public double getSalary() {  
  56.         return salary;  
  57.     }  
  58.   
  59.     public void setSalary(double salary) {  
  60.         this.salary = salary;  
  61.     }  
  62.   
  63.     public Person(String name, int age) {  
  64.         this.name = name;  
  65.         this.age = age;  
  66.     }  
  67.   
  68.     public Person(String name, int age, boolean sex, Date birthday,  
  69.             String word, double salary) {  
  70.         super();  
  71.         this.name = name;  
  72.         this.age = age;  
  73.         this.sex = sex;  
  74.         this.birthday = birthday;  
  75.         this.word = word;  
  76.         this.salary = salary;  
  77.     }  
  78.   
  79.     public Person() {  
  80.     }  
  81.   
  82.     @Override  
  83.     public String toString() {  
  84.         return "Person [name=" + name + ", age=" + age + ", sex=" + sex  
  85.                 + ", birthday=" + birthday + ", word=" + word + ", salary="  
  86.                 + salary + "]";  
  87.     }  
  88.   
  89. }  

[java] view plaincopy
  1. public class Demo {  
  2.     public static void main(String[] args) {  
  3.   
  4.         writeJsonObject();  
  5.   
  6.         // readJsonObject();  
  7.     }  
  8.   
  9.     // 直接写入一个对象(所谓序列化)  
  10.     public static void writeJsonObject() {  
  11.         ObjectMapper mapper = new ObjectMapper();  
  12.         Person person = new Person("nomouse"25truenew Date(), "程序员",  
  13.                 2500.0);  
  14.         try {  
  15.             mapper.writeValue(new File("c:/person.json"), person);  
  16.         } catch (JsonGenerationException e) {  
  17.             e.printStackTrace();  
  18.         } catch (JsonMappingException e) {  
  19.             e.printStackTrace();  
  20.         } catch (IOException e) {  
  21.             e.printStackTrace();  
  22.         }  
  23.     }  
  24.   
  25.     // 直接将一个json转化为对象(所谓反序列化)  
  26.     public static void readJsonObject() {  
  27.         ObjectMapper mapper = new ObjectMapper();  
  28.   
  29.         try {  
  30.             Person person = mapper.readValue(new File("c:/person.json"),  
  31.                     Person.class);  
  32.             System.out.println(person.toString());  
  33.         } catch (JsonParseException e) {  
  34.             e.printStackTrace();  
  35.         } catch (JsonMappingException e) {  
  36.             e.printStackTrace();  
  37.         } catch (IOException e) {  
  38.             e.printStackTrace();  
  39.         }  
  40.     }  
  41.   
  42.   
  43. }  

0 0
原创粉丝点击