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
  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
  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
  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
  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. }  
  73. 原文地址http://blog.csdn.net/accountwcx/article/details/24585987
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值