Json轉換利器Gson之實例二-Gson註解和GsonBuilder

有時候我們不需要把實體的所有屬性都導出,只想把一部分屬性導出爲Json.

有時候我們的實體類會隨着版本的升級而修改.

有時候我們想對輸出的json默認排好格式.

... ...

請看下面的例子吧:

實體類:

[java] view plaincopy
  1. import java.util.Date;  
  2.   
  3. import com.google.gson.annotations.Expose;  
  4. import com.google.gson.annotations.SerializedName;  
  5.   
  6. public class Student {  
  7.     private int id;  
  8.       
  9.     @Expose  
  10.     private String name;  
  11.       
  12.     @Expose  
  13.     @SerializedName("bir")  
  14.     private Date birthDay;  
  15.   
  16.     public int getId() {  
  17.         return id;  
  18.     }  
  19.   
  20.     public void setId(int id) {  
  21.         this.id = id;  
  22.     }  
  23.   
  24.     public String getName() {  
  25.         return name;  
  26.     }  
  27.   
  28.     public void setName(String name) {  
  29.         this.name = name;  
  30.     }  
  31.   
  32.     public Date getBirthDay() {  
  33.         return birthDay;  
  34.     }  
  35.   
  36.     public void setBirthDay(Date birthDay) {  
  37.         this.birthDay = birthDay;  
  38.     }  
  39.   
  40.     @Override  
  41.     public String toString() {  
  42.         return "Student [birthDay=" + birthDay + ", id=" + id + ", name="  
  43.                 + name + "]";  
  44.     }  
  45.   
  46. }  

測試類:

[java] view plaincopy
  1. import java.util.ArrayList;  
  2. import java.util.Date;  
  3. import java.util.List;  
  4.   
  5. import com.google.gson.FieldNamingPolicy;  
  6. import com.google.gson.Gson;  
  7. import com.google.gson.GsonBuilder;  
  8. import com.google.gson.reflect.TypeToken;  
  9.   
  10. public class GsonTest2 {  
  11.   
  12.     public static void main(String[] args) {  
  13.         //注意這裏的Gson的構建方式爲GsonBuilder,區別於test1中的Gson gson = new Gson();  
  14.         Gson gson = new GsonBuilder()  
  15.         .excludeFieldsWithoutExposeAnnotation() //不導出實體中沒有用@Expose註解的屬性  
  16.         .enableComplexMapKeySerialization() //支持Map的key爲複雜對象的形式  
  17.         .serializeNulls().setDateFormat("yyyy-MM-dd HH:mm:ss:SSS")//時間轉化爲特定格式    
  18.         .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)//會把字段首字母大寫,
  19. //注:對於實體上使用了@SerializedName註解的不會生效.  
  20.         .setPrettyPrinting() //對json結果格式化.  
  21.         .setVersion(1.0)    //有的字段不是一開始就有的,會隨着版本的升級添加進來,
  22. //那麼在進行序列化和返序列化的時候就會根據版本號來選擇是否要序列化.  
  23.                             //@Since(版本號)能完美地實現這個功能.還的字段可能,隨着版本的升級而刪除,那麼  
  24.                             //@Until(版本號)也能實現這個功能,GsonBuilder.setVersion(double)方法需要調用.  
  25.         .create();  
  26.           
  27.           
  28.   
  29.         Student student1 = new Student();  
  30.         student1.setId(1);  
  31.         student1.setName("李坤");  
  32.         student1.setBirthDay(new Date());  
  33.   
  34.         // //////////////////////////////////////////////////////////  
  35.         System.out.println("----------簡單對象之間的轉化-------------");  
  36.         // 簡單的bean轉爲json  
  37.         String s1 = gson.toJson(student1);  
  38.         System.out.println("簡單Bean轉化爲Json===" + s1);  
  39.   
  40.         // json轉爲簡單Bean  
  41.         Student student = gson.fromJson(s1, Student.class);  
  42.         System.out.println("Json轉爲簡單Bean===" + student);  
  43.         // //////////////////////////////////////////////////////////  
  44.   
  45.         Student student2 = new Student();  
  46.         student2.setId(2);  
  47.         student2.setName("曹貴生");  
  48.         student2.setBirthDay(new Date());  
  49.   
  50.         Student student3 = new Student();  
  51.         student3.setId(3);  
  52.         student3.setName("柳波");  
  53.         student3.setBirthDay(new Date());  
  54.   
  55.         List<Student> list = new ArrayList<Student>();  
  56.         list.add(student1);  
  57.         list.add(student2);  
  58.         list.add(student3);  
  59.   
  60.         System.out.println("----------帶泛型的List之間的轉化-------------");  
  61.         // 帶泛型的list轉化爲json  
  62.         String s2 = gson.toJson(list);  
  63.         System.out.println("帶泛型的list轉化爲json==" + s2);  
  64.   
  65.         // json轉爲帶泛型的list  
  66.         List<Student> retList = gson.fromJson(s2,  
  67.                 new TypeToken<List<Student>>() {  
  68.                 }.getType());  
  69.         for (Student stu : retList) {  
  70.             System.out.println(stu);  
  71.         }  
  72.           
  73.     }  
  74. }  

輸出結果:

[plain] view plaincopy
  1. ----------簡單對象之間的轉化-------------  
  2. 簡單Bean轉化爲Json==={  
  3.   "Name": "李坤",  
  4.   "bir": "2012-06-22 21:26:40:592"  
  5. }  
  6. Json轉爲簡單Bean===Student [birthDay=Fri Jun 22 21:26:40 CST 2012, id=0, name=李坤]  
  7. ----------帶泛型的List之間的轉化-------------  
  8. 帶泛型的list轉化爲json==[  
  9.   {  
  10.     "Name": "李坤",  
  11.     "bir": "2012-06-22 21:26:40:592"  
  12.   },  
  13.   {  
  14.     "Name": "曹貴生",  
  15.     "bir": "2012-06-22 21:26:40:625"  
  16.   },  
  17.   {  
  18.     "Name": "柳波",  
  19.     "bir": "2012-06-22 21:26:40:625"  
  20.   }  
  21. ]  
  22. Student [birthDay=Fri Jun 22 21:26:40 CST 2012, id=0, name=李坤]  
  23. Student [birthDay=Fri Jun 22 21:26:40 CST 2012, id=0, name=曹貴生]  
  24. Student [birthDay=Fri Jun 22 21:26:40 CST 2012, id=0, name=柳波] 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章