`

Json转换利器Gson之实例二-Gson注解和GsonBuilder

阅读更多

有时候我们不需要把实体的所有属性都导出,只想把一部分属性导出为Json.

 

有时候我们的实体类会随着版本的升级而修改.

 

有时候我们想对输出的json默认排好格式.

 

... ...

 

请看下面的例子吧:

 

实体类:

 

  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. }  

 

测试类:

 

 

 

  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)//会把字段首字母大写,注:对于实体上使用了@SerializedName注解的不会生效.  
  19.         .setPrettyPrinting() //对json结果格式化.  
  20.         .setVersion(1.0)    //有的字段不是一开始就有的,会随着版本的升级添加进来,那么在进行序列化和返序列化的时候就会根据版本号来选择是否要序列化.  
  21.                             //@Since(版本号)能完美地实现这个功能.还的字段可能,随着版本的升级而删除,那么  
  22.                             //@Until(版本号)也能实现这个功能,GsonBuilder.setVersion(double)方法需要调用.  
  23.         .create();  
  24.           
  25.           
  26.   
  27.         Student student1 = new Student();  
  28.         student1.setId(1);  
  29.         student1.setName("李坤");  
  30.         student1.setBirthDay(new Date());  
  31.   
  32.         // //////////////////////////////////////////////////////////  
  33.         System.out.println("----------简单对象之间的转化-------------");  
  34.         // 简单的bean转为json  
  35.         String s1 = gson.toJson(student1);  
  36.         System.out.println("简单Bean转化为Json===" + s1);  
  37.   
  38.         // json转为简单Bean  
  39.         Student student = gson.fromJson(s1, Student.class);  
  40.         System.out.println("Json转为简单Bean===" + student);  
  41.         // //////////////////////////////////////////////////////////  
  42.   
  43.         Student student2 = new Student();  
  44.         student2.setId(2);  
  45.         student2.setName("曹贵生");  
  46.         student2.setBirthDay(new Date());  
  47.   
  48.         Student student3 = new Student();  
  49.         student3.setId(3);  
  50.         student3.setName("柳波");  
  51.         student3.setBirthDay(new Date());  
  52.   
  53.         List<Student> list = new ArrayList<Student>();  
  54.         list.add(student1);  
  55.         list.add(student2);  
  56.         list.add(student3);  
  57.   
  58.         System.out.println("----------带泛型的List之间的转化-------------");  
  59.         // 带泛型的list转化为json  
  60.         String s2 = gson.toJson(list);  
  61.         System.out.println("带泛型的list转化为json==" + s2);  
  62.   
  63.         // json转为带泛型的list  
  64.         List<Student> retList = gson.fromJson(s2,  
  65.                 new TypeToken<List<Student>>() {  
  66.                 }.getType());  
  67.         for (Student stu : retList) {  
  68.             System.out.println(stu);  
  69.         }  
  70.           
  71.     }  
  72. }  


输出结果:

 

 

  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=柳波]  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics