java利器F Json之Gson(二)

1.今天继续看昨天的gson利器:

 

环境:

1.Student.java:

package com.wds.json;

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 Student() {
		super();
	}
	public Student(int id, String name, Date birthDay) {
		super();
		this.id = id;
		this.name = name;
		this.birthDay = birthDay;
	}
	@Override
	public String toString() {
		return "Student [birthDay="+birthDay+",id="+id+",name="+name+"]";
	}
	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;
	}
}

 

2.Point.java:

package com.wds.json;

public class Point {
	private int x;
	private int y;
	
	public Point() {
		super();
	}
	public Point(int x, int y) {
		super();
		this.x = x;
		this.y = y;
	}
	
	@Override
	public String toString() {
		return "Point[x="+x+",y="+y+"]";
	}
	public int getX() {
		return x;
	}
	public void setX(int x) {
		this.x = x;
	}
	public int getY() {
		return y;
	}
	public void setY(int y) {
		this.y = y;
	}
}

 

 3.测试类:

package com.wds.json;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
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) {
  //第1组
  // Gson gson = new Gson();
  //第2组
  Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation() // 不导出实体中没有用@Expose注解的属性
    .serializeNulls().setDateFormat("yyyy-MM-dd HH:mm:ss:SSS")// 时间转化为特定格式
    .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)// 会把字段首字母大写,注:对于实体上使用了@SerializedName注解的不会生效.
    .setPrettyPrinting() // 对json结果格式化
    // 有的字段不是一开始就有的,会随着版本的升级添加进来,那么在进行序列化和返序列化的时候就会根据版本号来选择是否要序列化.
    // @Since(版本号)能完美地实现这个功能.还的字段可能,随着版本的升级而删除,那么
    // @Until(版本号)也能实现这个功能,GsonBuilder.setVersion(double)方法需要调用.
    .setVersion(1.0).create();
  forSimple(gson);
  forSet(gson);
 }
private static void forSet(Gson gson) {
  Student s1 = new Student();
  s1.setId(1);
  s1.setName("李坤");
  s1.setBirthDay(new Date());
  Student s2 = new Student();
  s2.setId(2);
  s2.setName("徐兵");
  s2.setBirthDay(new Date());
  Student s3 = new Student();
  s3.setId(1);
  s3.setName("沈东");
  s3.setBirthDay(new Date());
  Set<Student> ss = new HashSet<Student>();
  ss.add(s1);
  ss.add(s2);
  ss.add(s3);
  String json = gson.toJson(ss);
  System.out.println("set = > json:" + json);
  List<Student> retList = gson.fromJson(json,
    new TypeToken<List<Student>>() {
    }.getType());
  for (Student s : retList) {
   System.out.println("Student[x]:" + s);
  }
 }
private static void forSimple(Gson gson) {
  Student student1 = new Student();
  student1.setId(1);
  student1.setName("李坤");
  student1.setBirthDay(new Date());

  // 简单的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);
 }
}

 

3.第一组运行结果:

bean = > json:{"id":1,"name":"李坤","bir":"Jun 19, 2013 9:26:04 PM"}
json = > bean:Student [birthDay=Wed Jun 19 21:26:04 CST 2013,id=1,name=李坤]
set = > json:[{"id":2,"name":"徐兵","bir":"Jun 19, 2013 9:26:04 PM"},{"id":1,"name":"沈东","bir":"Jun 19, 2013 9:26:04 PM"},{"id":1,"name":"李坤","bir":"Jun 19, 2013 9:26:04 PM"}]
Student[x]:Student [birthDay=Wed Jun 19 21:26:04 CST 2013,id=2,name=徐兵]
Student[x]:Student [birthDay=Wed Jun 19 21:26:04 CST 2013,id=1,name=沈东]
Student[x]:Student [birthDay=Wed Jun 19 21:26:04 CST 2013,id=1,name=李坤]

 

第二组运行结果:

bean = > json:{
  "Name": "李坤",
  "bir": "2013-06-19 21:33:40:458"
}
json = > bean:Student [birthDay=Wed Jun 19 21:33:40 CST 2013,id=0,name=李坤]
set = > json:[
  {
    "Name": "沈东",
    "bir": "2013-06-19 21:33:40:498"
  },
  {
    "Name": "李坤",
    "bir": "2013-06-19 21:33:40:498"
  },
  {
    "Name": "徐兵",
    "bir": "2013-06-19 21:33:40:498"
  }
]
Student[x]:Student [birthDay=Wed Jun 19 21:33:40 CST 2013,id=0,name=沈东]
Student[x]:Student [birthDay=Wed Jun 19 21:33:40 CST 2013,id=0,name=李坤]
Student[x]:Student [birthDay=Wed Jun 19 21:33:40 CST 2013,id=0,name=徐兵]

 

你可能感兴趣的:(java)