Gson注解和GsonBuilder

来源:互联网 发布:猫眼和微影合并.知乎 编辑:程序博客网 时间:2024/06/02 09:13

请看下面的例子吧:

实体类:

[java] view plaincopyprint?
  1. import java.util.Date;
  2. import com.google.gson.annotations.Expose;
  3. import com.google.gson.annotations.SerializedName;
  4. public class Student {
  5. privateint 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 plaincopyprint?
  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 plaincopyprint?
  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=柳波]  
0 0