第三方類庫-JSON-Jackson

原文出處:http://blog.csdn.net/zmx729618/article/details/52161069


一、準備工作

首先去官網下載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(下載地址

  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("[email protected]");  
  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":"[email protected]"}  
  29.         String json = mapper.writeValueAsString(user);  
  30.         System.out.println(json);  
  31.           
  32.         //Java集合轉JSON  
  33.         //輸出結果:[{"name":"小民","age":20,"birthday":844099200000,"email":"[email protected]"}]  
  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\":\"[email protected]\"}";  
  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上查看代碼片派生到我的代碼片在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("[email protected]");  
  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":"[email protected]"}  
  71.     }  
  72. }  
發佈了3 篇原創文章 · 獲贊 3 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章