有时候我们不需要把实体的所有属性都导出,只想把一部分属性导出为Json.
有时候我们的实体类会随着版本的升级而修改.
有时候我们想对输出的json默认排好格式.
... ...
请看下面的例子吧:
实体类:
[java] view
plaincopy
- import java.util.Date;
- import com.google.gson.annotations.Expose;
- import com.google.gson.annotations.SerializedName;
- public class Student {
- private int id;
- @Expose
- private String name;
- @Expose
- @SerializedName("bir")
- private Date birthDay;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Date getBirthDay() {
- return birthDay;
- }
- public void setBirthDay(Date birthDay) {
- this.birthDay = birthDay;
- }
- @Override
- public String toString() {
- return "Student [birthDay=" + birthDay + ", id=" + id + ", name="
- + name + "]";
- }
- }
测试类:
[java] view
plaincopy
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import com.google.gson.FieldNamingPolicy;
- import com.google.gson.Gson;
- import com.google.gson.GsonBuilder;
- import com.google.gson.reflect.TypeToken;
- public class GsonTest2 {
- public static void main(String[] args) {
- //注意这里的Gson的构建方式为GsonBuilder,区别于test1中的Gson gson = new Gson();
- Gson gson = new GsonBuilder()
- .excludeFieldsWithoutExposeAnnotation() //不导出实体中没有用@Expose注解的属性
- .enableComplexMapKeySerialization() //支持Map的key为复杂对象的形式
- .serializeNulls().setDateFormat("yyyy-MM-dd HH:mm:ss:SSS")//时间转化为特定格式
- .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)//会把字段首字母大写,注:对于实体上使用了@SerializedName注解的不会生效.
- .setPrettyPrinting() //对json结果格式化.
- .setVersion(1.0) //有的字段不是一开始就有的,会随着版本的升级添加进来,那么在进行序列化和返序列化的时候就会根据版本号来选择是否要序列化.
- //@Since(版本号)能完美地实现这个功能.还的字段可能,随着版本的升级而删除,那么
- //@Until(版本号)也能实现这个功能,GsonBuilder.setVersion(double)方法需要调用.
- .create();
- Student student1 = new Student();
- student1.setId(1);
- student1.setName("李坤");
- student1.setBirthDay(new Date());
- // //////////////////////////////////////////////////////////
- System.out.println("----------简单对象之间的转化-------------");
- // 简单的bean转为json
- String s1 = gson.toJson(student1);
- System.out.println("简单Bean转化为Json===" + s1);
- // json转为简单Bean
- Student student = gson.fromJson(s1, Student.class);
- System.out.println("Json转为简单Bean===" + student);
- // //////////////////////////////////////////////////////////
- Student student2 = new Student();
- student2.setId(2);
- student2.setName("曹贵生");
- student2.setBirthDay(new Date());
- Student student3 = new Student();
- student3.setId(3);
- student3.setName("柳波");
- student3.setBirthDay(new Date());
- List<Student> list = new ArrayList<Student>();
- list.add(student1);
- list.add(student2);
- list.add(student3);
- System.out.println("----------带泛型的List之间的转化-------------");
- // 带泛型的list转化为json
- String s2 = gson.toJson(list);
- System.out.println("带泛型的list转化为json==" + s2);
- // json转为带泛型的list
- List<Student> retList = gson.fromJson(s2,
- new TypeToken<List<Student>>() {
- }.getType());
- for (Student stu : retList) {
- System.out.println(stu);
- }
- }
- }
输出结果:
[plain] view
plaincopy
- ----------简单对象之间的转化-------------
- 简单Bean转化为Json==={
- "Name": "李坤",
- "bir": "2012-06-22 21:26:40:592"
- }
- Json转为简单Bean===Student [birthDay=Fri Jun 22 21:26:40 CST 2012, id=0, name=李坤]
- ----------带泛型的List之间的转化-------------
- 带泛型的list转化为json==[
- {
- "Name": "李坤",
- "bir": "2012-06-22 21:26:40:592"
- },
- {
- "Name": "曹贵生",
- "bir": "2012-06-22 21:26:40:625"
- },
- {
- "Name": "柳波",
- "bir": "2012-06-22 21:26:40:625"
- }
- ]
- Student [birthDay=Fri Jun 22 21:26:40 CST 2012, id=0, name=李坤]
- Student [birthDay=Fri Jun 22 21:26:40 CST 2012, id=0, name=曹贵生]
- Student [birthDay=Fri Jun 22 21:26:40 CST 2012, id=0, name=柳波]
Json转换利器Gson之实例一-简单对象转化和带泛型的List转化 (http://blog.csdn.net/lk_blog/article/details/7685169)
Json转换利器Gson之实例二-Gson注解和GsonBuilder (http://blog.csdn.net/lk_blog/article/details/7685190)
Json转换利器Gson之实例三-Map处理(上) (http://blog.csdn.net/lk_blog/article/details/7685210)
Json转换利器Gson之实例四-Map处理(下) (http://blog.csdn.net/lk_blog/article/details/7685224)
Json转换利器Gson之实例五-实际开发中的特殊需求处理 (http://blog.csdn.net/lk_blog/article/details/7685237)
Json转换利器Gson之实例六-注册TypeAdapter及处理Enum类型 (http://blog.csdn.net/lk_blog/article/details/7685347)
实例代码下载: http://download.csdn.net/detail/lk_blog/4387822
时间: 2024-10-08 08:16:03