国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > 互联网 > Json转换利器Gson之实例二-Gson注解和GsonBuilder

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

来源:程序员人生   发布时间:2015-04-22 08:38:52 阅读次数:4025次

有时候我们不需要把实体的所有属性都导出,只想把1部份属性导出为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)//会把字段首字母大写,注:对实体上使用了@SerializedName注解的不会生效.  
  19.         .setPrettyPrinting() //对json结果格式化.  
  20.         .setVersion(1.0)    //有的字段不是1开始就有的,会随着版本的升级添加进来,那末在进行序列化和返序列化的时候就会根据版本号来选择是不是要序列化.  
  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.       
    生活不易,码农辛苦
    如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
    程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生