天天看點

Json轉換利器Gson之執行個體二-Gson注解和GsonBuilder

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

有時候我們的實體類會随着版本的更新而修改.

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

... ...

請看下面的例子吧:

實體類:

[java] view plain copy

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

測試類:

[java] view plain copy

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

輸出結果:

[plain] view plain copy

  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=柳波]  
Json轉換利器Gson之執行個體二-Gson注解和GsonBuilder