Gson (GitHub:https://github.com/google/gson)是 Google 提供的用來在 Java 對象和 JSON 數(shù)據(jù)之間進行映射的 Java 類庫。可以將一個 JSON 字符串轉(zhuǎn)成一個 Java 對象,或者反過來。
Gson里最重要的對象有2個Gson 和 GsonBuilder。
Gson有2個最基本的方法
(1)toJson() – 轉(zhuǎn)換java 對象到JSON
(2)fromJson() – 轉(zhuǎn)換JSON到java對象
編寫實體類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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)); } } |
輸出結果:
1
|
{"name":"People","age":20,"setName":true} |
這只是最簡單的Gson的使用。如果我們需要將bool類型的屬性setName在轉(zhuǎn)換成json的時候不轉(zhuǎn)換,怎么實現(xiàn)呢?
在Gson的包中找半天,發(fā)現(xiàn)com.google.gson包下面有這么一個接口:ExclusionStrategy ,雖然不清楚是干什么的,但是根據(jù)名字,可以推斷,這個接口是用來設置Gson轉(zhuǎn)換的排除策略的,于是在官網(wǎng)http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/index.html查了一下這個接口,發(fā)現(xiàn)只要實現(xiàn)這個接口,并將實現(xiàn)類的對象塞給Gson,在轉(zhuǎn)換成json的時候,Gson就會過濾掉指定的類或者屬性。于是有了下面的代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
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對象的創(chuàng)建有兩種方式:new Gson()表示使用默認的配置創(chuàng)建一個Gson對象,而如果使用GsonBuilder.create()方法創(chuàng)建,則可以自定義一些設置,這主要是為了使創(chuàng)建的Gson更適合于某些特定的情況。上例中第一段藍色的代碼創(chuàng)建了一個Gson對象,這個對象擁有對以“set”字樣開頭的屬性的過濾的配置(如果需要過濾掉某種類型,則重寫ExclusionStrategy接口的shouldSkipClass(Class<?> clazz)方法即可,如果需要過濾掉多種情況,則可以多創(chuàng)建幾個ExclusionStrategy的實現(xiàn)類對象,并在創(chuàng)建Gson對象的時候設置進去即可),因此在本例中,將People對象轉(zhuǎn)換成Json的時候,屬性setName將被過濾掉。由于json1中沒有屬性setName,所以將json1反序列化成People對象的時候,boolean類型的setName就沒有了值,所以打印的時候取了boolean類型的默認值。于是有了以下結果:
1
2
3
4
|
{"name":"People","age":20} {"name":"People","age":20,"setName":true} name=People age=20 setName=false name=People age=20 setName=true |
Gson還支持使用注解,在com.google.gson.annotation包中,有幾個注解Expose, SerializedName, Since和Until,他們各有各的作用,下面使用官方例子介紹常用的注解:
Expose:
此注解作用在屬性上,表明當序列化和反序列化的時候,這個屬性將會暴露給Gson對象。這個注解只有當創(chuàng)建Gson對象時使用GsonBuilder方式創(chuàng)建并調(diào)用了GsonBuilder.excludeFieldsWithoutExposeAnnotation() 方法的時候才有效,否則無效。下面是一個介紹@Expose注解如何使用的例子:
1
2
3
4
5
6
|
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()的方式創(chuàng)建Gson對象,toJson()方法和fromJson() 方法在序列化和反序列化的時候?qū)僮鬟@4個屬性。然而,如果你使用 Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create()來創(chuàng)建Gson對象,Gson 的 toJson() 和 fromJson() 方法將會排除掉 password 字段,這是因為 password 字段沒有被注解 @Expose 所標記。 這個 Gson 對象同樣會排除 lastName 和 emailAddress 字段,因為注解@Expose的屬性 serialize 被設置成了 false。類似的,Gson 將會在反序列化時排除掉 emailAddress 字段,因為 deserialize被設置成了 false。
SerializedName:
此注解作用在屬性上,表明這個屬性在序列化成Json的時候,需要將名字序列化成注解的value屬性指定的值。
這個注解將會覆蓋任何的FieldNamingPolicy, 包括默認的命名策略。下面是一個介紹@SerializedName注解如何使用的例子: ,
1
2
3
4
5
6
7
8
|
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; } } |
下面的代碼展示了序列化上面這個測試類的結果:
1
2
3
4
|
SomeClassWithFields objectToSerialize = new SomeClassWithFields( "a" , "b" ); Gson gson = new Gson(); String jsonRepresentation = gson.toJson(objectToSerialize); System.out.println(jsonRepresentation); |
執(zhí)行結果是:
1
|
{"name":"a","someOtherField":"b"} |
由此可見,屬性"someField"已經(jīng)被序列化成了"name"。
注意:在@SerializedName的value中指定的屬性名必須為有效的Json屬性名。