Java下利用Jackson进行JSON解析和序列化

Java下常见的Json类库有Gson、JSON-lib和Jackson等,Jackson相对来说比较高效,在项目中主要使用Jackson进行JSON和Java对象转换,下面给出一些Jackson的JSON操作方法。

一、准备工作

首先去官网下载Jackson工具包,下载地址http://wiki.fasterxml.com/JacksonDownload。Jackson有1.x系列和2.x系列,截止目前2.x系列的最新版本是2.2.3,2.x系列有3个jar包需要下载:

jackson-core-2.2.3.jar(核心jar包,下载地址

jackson-annotations-2.2.3.jar(该包提供Json注解支持,下载地址

jackson-databind-2.2.3.jar(下载地址

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. //JSON序列化和反序列化使用的User类  
  2. import java.util.Date;  
  3.   
  4. public class User {  
  5.     private String name;  
  6.     private Integer age;  
  7.     private Date birthday;  
  8.     private String email;  
  9.       
  10.     public String getName() {  
  11.         return name;  
  12.     }  
  13.     public void setName(String name) {  
  14.         this.name = name;  
  15.     }  
  16.       
  17.     public Integer getAge() {  
  18.         return age;  
  19.     }  
  20.     public void setAge(Integer age) {  
  21.         this.age = age;  
  22.     }  
  23.       
  24.     public Date getBirthday() {  
  25.         return birthday;  
  26.     }  
  27.     public void setBirthday(Date birthday) {  
  28.         this.birthday = birthday;  
  29.     }  
  30.       
  31.     public String getEmail() {  
  32.         return email;  
  33.     }  
  34.     public void setEmail(String email) {  
  35.         this.email = email;  
  36.     }  
  37. }  

二、JAVA对象转JSON[JSON序列化]

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. import java.io.IOException;  
  2. import java.text.ParseException;  
  3. import java.text.SimpleDateFormat;  
  4.   
  5. import com.fasterxml.jackson.databind.ObjectMapper;  
  6.   
  7. public class JacksonDemo {  
  8.     public static void main(String[] args) throws ParseException, IOException {  
  9.         User user = new User();  
  10.         user.setName("小民");   
  11.         user.setEmail("xiaomin@sina.com");  
  12.         user.setAge(20);  
  13.           
  14.         SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");  
  15.         user.setBirthday(dateformat.parse("1996-10-01"));         
  16.           
  17.         /** 
  18.          * ObjectMapper是JSON操作的核心,Jackson的所有JSON操作都是在ObjectMapper中实现。 
  19.          * ObjectMapper有多个JSON序列化的方法,可以把JSON字符串保存File、OutputStream等不同的介质中。 
  20.          * writeValue(File arg0, Object arg1)把arg1转成json序列,并保存到arg0文件中。 
  21.          * writeValue(OutputStream arg0, Object arg1)把arg1转成json序列,并保存到arg0输出流中。 
  22.          * writeValueAsBytes(Object arg0)把arg0转成json序列,并把结果输出成字节数组。 
  23.          * writeValueAsString(Object arg0)把arg0转成json序列,并把结果输出成字符串。 
  24.          */  
  25.         ObjectMapper mapper = new ObjectMapper();  
  26.           
  27.         //User类转JSON  
  28.         //输出结果:{"name":"小民","age":20,"birthday":844099200000,"email":"xiaomin@sina.com"}  
  29.         String json = mapper.writeValueAsString(user);  
  30.         System.out.println(json);  
  31.           
  32.         //Java集合转JSON  
  33.         //输出结果:[{"name":"小民","age":20,"birthday":844099200000,"email":"xiaomin@sina.com"}]  
  34.         List<User> users = new ArrayList<User>();  
  35.         users.add(user);  
  36.         String jsonlist = mapper.writeValueAsString(users);  
  37.         System.out.println(jsonlist);  
  38.     }  
  39. }  

三、JSON转Java类[JSON反序列化]

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. import java.io.IOException;  
  2. import java.text.ParseException;  
  3. import com.fasterxml.jackson.databind.ObjectMapper;  
  4.   
  5. public class JacksonDemo {  
  6.     public static void main(String[] args) throws ParseException, IOException {  
  7.         String json = "{"name":"小民","age":20,"birthday":844099200000,"email":"xiaomin@sina.com"}";  
  8.           
  9.         /** 
  10.          * ObjectMapper支持从byte[]、File、InputStream、字符串等数据的JSON反序列化。 
  11.          */  
  12.         ObjectMapper mapper = new ObjectMapper();  
  13.         User user = mapper.readValue(json, User.class);  
  14.         System.out.println(user);  
  15.     }  
  16. }  

四、JSON注解

Jackson提供了一系列注解,方便对JSON序列化和反序列化进行控制,下面介绍一些常用的注解。

@JsonIgnore 此注解用于属性上,作用是进行JSON操作时忽略该属性。

@JsonFormat 此注解用于属性上,作用是把Date类型直接转化为想要的格式,如@JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss")。

@JsonProperty 此注解用于属性上,作用是把该属性的名称序列化为另外一个名称,如把trueName属性序列化为name,@JsonProperty("name")。

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
    1. import java.util.Date;  
    2. import com.fasterxml.jackson.annotation.*;  
    3.   
    4. public class User {  
    5.     private String name;  
    6.       
    7.     //不JSON序列化年龄属性  
    8.     @JsonIgnore   
    9.     private Integer age;  
    10.       
    11.     //格式化日期属性  
    12.     @JsonFormat(pattern = "yyyy年MM月dd日")  
    13.     private Date birthday;  
    14.       
    15.     //序列化email属性为mail  
    16.     @JsonProperty("mail")  
    17.     private String email;  
    18.       
    19.     public String getName() {  
    20.         return name;  
    21.     }  
    22.     public void setName(String name) {  
    23.         this.name = name;  
    24.     }  
    25.       
    26.     public Integer getAge() {  
    27.         return age;  
    28.     }  
    29.     public void setAge(Integer age) {  
    30.         this.age = age;  
    31.     }  
    32.       
    33.     public Date getBirthday() {  
    34.         return birthday;  
    35.     }  
    36.     public void setBirthday(Date birthday) {  
    37.         this.birthday = birthday;  
    38.     }  
    39.       
    40.     public String getEmail() {  
    41.         return email;  
    42.     }  
    43.     public void setEmail(String email) {  
    44.         this.email = email;  
    45.     }  
    46. }  
    47.   
    48.   
    49.   
    50. import java.io.IOException;  
    51. import java.text.ParseException;  
    52. import java.text.SimpleDateFormat;  
    53.   
    54. import com.fasterxml.jackson.databind.ObjectMapper;  
    55.   
    56. public class JacksonDemo {  
    57.   
    58.     public static void main(String[] args) throws ParseException, IOException {  
    59.         User user = new User();  
    60.         user.setName("小民");   
    61.         user.setEmail("xiaomin@sina.com");  
    62.         user.setAge(20);  
    63.           
    64.         SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");  
    65.         user.setBirthday(dateformat.parse("1996-10-01"));         
    66.           
    67.         ObjectMapper mapper = new ObjectMapper();  
    68.         String json = mapper.writeValueAsString(user);  
    69.         System.out.println(json);  
    70.         //输出结果:{"name":"小民","birthday":"1996年09月30日","mail":"xiaomin@sina.com"}  
    71.     }  
    72. }  
原文地址:https://www.cnblogs.com/linjiaxin/p/6240640.html