Java的JSON轉換類庫GSON的基礎使用教程_java

來源:互聯網
上載者:User

Gson (GitHub:https://github.com/google/gson)是 Google 提供的用來在 Java 對象和 JSON 資料之間進行映射的 Java 類庫。可以將一個 JSON 字串轉成一個 Java 對象,或者反過來。
Gson裡最重要的對象有2個Gson 和 GsonBuilder。
Gson有2個最基本的方法
(1)toJson() – 轉換java 對象到JSON
(2)fromJson() – 轉換JSON到java對象

編寫實體類:

public class People {  String name;  int age;  boolean setName;  public String getName() {    return name;  }  public void setName(String name) {    this.name = name;  }  public int getAge() {    return age;  }  public void setAge(int age) {    this.age = age;  }  public boolean getSetName() {    return setName;  }  public void setSetName(boolean setName) {    this.setName = setName;  }  @Override  public String toString() {    return "name=" + name + " age=" + age + " setName=" +setName;  }}

編寫測試類別GsonTest

import com.google.gson.ExclusionStrategy;import com.google.gson.FieldAttributes;import com.google.gson.Gson;import com.google.gson.GsonBuilder;/** * Convert java object to json. */public class GsonTest {  public static void main(String[] args) {    People p = new People();    p.setAge(20);    p.setName("People");    p.setSetName(true);    Gson gson = new Gson();    System.out.println(gson.toJson(p));  }}

輸出結果:

{"name":"People","age":20,"setName":true}

這隻是最簡單的Gson的使用。如果我們需要將bool類型的屬性setName在轉換成json的時候不轉換,怎麼實現呢?

在Gson的包中找半天,發現com.google.gson包下面有這麼一個介面:ExclusionStrategy ,雖然不清楚是幹什麼的,但是根據名字,可以推斷,這個介面是用來設定Gson轉換的排除策略的,於是在官網http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/index.html查了一下這個介面,發現只要實現這個介面,並將實作類別的對象塞給Gson,在轉換成json的時候,Gson就會過濾掉指定的類或者屬性。於是有了下面的代碼:

import com.google.gson.ExclusionStrategy;import com.google.gson.FieldAttributes;import com.google.gson.Gson;import com.google.gson.GsonBuilder;/** * Convert java object to json, skip specific fileds. */public class GsonTest {  public static void main(String[] args) {    People p = new People();    p.setAge(20);    p.setName("People");    p.setSetName(true);    ExclusionStrategy excludeStrategy = new SetterExclusionStrategy();    Gson gson1 = new GsonBuilder()      .setExclusionStrategies(excludeStrategy)      .create();    Gson gson2 = new Gson();    String json1 = gson1.toJson(p);    String json2 = gson2.toJson(p);    System.out.println(json1);    System.out.println(json2);    People p1 = gson1.fromJson(json1, People.class);    People p2 = gson2.fromJson(json2, People.class);    System.out.println(p1);    System.out.println(p2);  }  private static class SetterExclusionStrategy implements ExclusionStrategy {    public boolean shouldSkipClass(Class<?> clazz) {      return false;    }    public boolean shouldSkipField(FieldAttributes f) {      return f.getName().startsWith("set");    }  }}

原來,Gson對象的建立有兩種方式:new Gson()表示使用預設的配置建立一個Gson對象,而如果使用GsonBuilder.create()方法建立,則可以自訂一些設定,這主要是為了使建立的Gson更適合於某些特定的情況。上例中第一段藍色的代碼建立了一個Gson對象,這個對象擁有對以“set”字樣開頭的屬性的過濾的配置(如果需要過濾掉某種類型,則重寫ExclusionStrategy介面的shouldSkipClass(Class<?> clazz)方法即可,如果需要過濾掉多種情況,則可以多建立幾個ExclusionStrategy的實作類別對象,並在建立Gson對象的時候設定進去即可),因此在本例中,將People對象轉換成Json的時候,屬性setName將被過濾掉。由於json1中沒有屬性setName,所以將json1還原序列化成People對象的時候,boolean類型的setName就沒有了值,所以列印的時候取了boolean類型的預設值。於是有了以下結果:

{"name":"People","age":20}{"name":"People","age":20,"setName":true}name=People age=20 setName=falsename=People age=20 setName=true

Gson還支援使用註解,在com.google.gson.annotation包中,有幾個註解Expose, SerializedName, Since和Until,他們各有各的作用,下面使用官方例子介紹常用的註解: 

Expose:

此註解作用在屬性上,表明當序列化和還原序列化的時候,這個屬性將會暴露給Gson對象。這個註解只有當建立Gson對象時使用GsonBuilder方式建立並調用了GsonBuilder.excludeFieldsWithoutExposeAnnotation() 方法的時候才有效,否則無效。下面是一個介紹@Expose註解如何使用的例子:

public class User {  @Expose private String firstName;  @Expose(serialize = false) private String lastName;  @Expose (serialize = false, deserialize = false) private String emailAddress;  private String password;}

如果你以new Gson()的方式建立Gson對象,toJson()方法和fromJson() 方法在序列化和還原序列化的時候將會操作這4個屬性。然而,如果你使用 Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create()來建立Gson對象,Gson 的 toJson() 和 fromJson() 方法將會排除掉 password 欄位,這是因為 password 欄位沒有被註解 @Expose 所標記。 這個 Gson 對象同樣會排除 lastName 和 emailAddress 欄位,因為註解@Expose的屬性 serialize 被設定成了 false。類似的,Gson 將會在還原序列化時排除掉 emailAddress 欄位,因為 deserialize被設定成了 false。

SerializedName:

此註解作用在屬性上,表明這個屬性在序列化成Json的時候,需要將名字序列化成註解的value屬性指定的值。

這個註解將會覆蓋任何的FieldNamingPolicy, 包括預設的命名策略。下面是一個介紹@SerializedName註解如何使用的例子: ,

public class SomeClassWithFields {  @SerializedName("name") private final String someField;  private final String someOtherField;  public SomeClassWithFields(String a, String b) {   this.someField = a;   this.someOtherField = b;  }}

下面的代碼展示了序列化上面這個測試類別的結果:

SomeClassWithFields objectToSerialize = new SomeClassWithFields("a", "b");Gson gson = new Gson();String jsonRepresentation = gson.toJson(objectToSerialize);System.out.println(jsonRepresentation);

執行結果是:

{"name":"a","someOtherField":"b"}

由此可見,屬性"someField"已經被序列化成了"name"。

注意:在@SerializedName的value中指定的屬性名稱必須為有效Json屬性名稱。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.