有時候我們不需要把實體的所有屬性都導出,只想把一部分屬性導出爲Json.
有時候我們的實體類會隨着版本的升級而修改.
有時候我們想對輸出的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 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 + "]";
- }
- }
測試類:
- 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);
- }
- }
- }
輸出結果:
- ----------簡單對象之間的轉化-------------
- 簡單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=柳波]