Java序列化jackson

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

  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("fasd");   
  11.         user.setEmail("fasdffdas");  
  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":"fasd","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":"fas","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\":\"adf\",\"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. }  


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值