GSON详细使用说明
GSON是一个google提供开源类库,可以实现java对象和json字符串的互相转换。Gson(又称Google Gson)是Google公司发布的一个开放源代码的Java库,主要用途为序列化Java对象为JSON字符串,或反序列化JSON字符串成Java对象。
课下可以看一下json开源组织的网站:
http://www.json.org/json-zh.html
建立java项目,先导入GSON的jar包。为了测试Gson的作用,我们定义两个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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
package com.bjsxt.bean;public class User { private int id; private String uname; private Address addr; private int age; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public Address getAddr() { return addr; } public void setAddr(Address addr) { this.addr = addr; } public User(int id, String uname, Address addr) { super(); this.id = id; this.uname = uname; this.addr = addr; } public User() { // TODO Auto-generated constructor stub }} |
|
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
|
package com.bjsxt.bean;public class Address { private String country; private String city; public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public Address(String country, String city) { super(); this.country = country; this.city = city; } public Address() { }} |
将java对象转成json字符串
|
1
2
3
4
5
6
7
|
public static void obj2JsonDemo(){ Address bj = new Address("中国","北京"); User u = new User(1001, "高淇", bj); Gson gson = new Gson(); System.out.println(gson.toJson(u));//{"id":1001,"uname":"高淇","addr":{"country":"中国","city":"北京"}} } |
结果如下:
|
1
|
{"id":1001,"uname":"高淇","addr":{"country":"中国","city":"北京"}} |
将容器中的对象转成json字符串
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public static void list2JsonDemo(){ Address bj = new Address("中国","北京"); User u = new User(1001, "高淇", bj); Address sh = new Address("中国","上海"); User u2 = new User(1002, "马士兵", sh); List list = new ArrayList(); list.add(u); list.add(u2); Gson gson = new Gson(); System.out.println(gson.toJson(list)); } |
执行该方法结果如下:
|
1
2
3
4
|
[ {"id":1001,"uname":"高淇","addr":{"country":"中国","city":"北京"}}, {"id":1002,"uname":"马士兵","addr":{"country":"中国","city":"上海"}}] |
JSON字符串转成java对象
|
1
2
3
4
5
6
|
public static void json2Obj(){ String s1 = "{\"id\":1001,\"uname\":\"高淇\",\"addr\":{\"country\":\"中国\",\"city\":\"北京\"}}"; Gson gson = new Gson(); User u = gson.fromJson(s1, User.class); System.out.println(u.getUname()); } |
JSON字符串转成容器对象
|
1
2
3
4
5
6
7
|
public static void json2List(){ String s1 = "[{\"id\":1001,\"uname\":\"高淇\",\"addr\":{\"country\":\"中国\",\"city\":\"北京\"}}," + "{\"id\":1002,\"uname\":\"马士兵\",\"addr\":{\"country\":\"中国\",\"city\":\"上海\"}}]"; Gson gson = new Gson(); List<User> list = gson.fromJson(s1, new TypeToken<List<User>>(){}.getType()); System.out.println(list.get(1).getUname()); } |
@Expose注解和@SerializedName注解的使用
使用Gson的@Expose注解和@SerializedName ,更加细微控制java对象转成json字符串。
@Expose控制是否序列化指定属性
@Expose(serialize=true)
private String uname;
@SerializedName控制java对象属性转成json对象的属性名:
@SerializedName("年龄")
private int age;
使用注解定义如下新的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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
package com.bjsxt.bean;import com.google.gson.annotations.Expose;import com.google.gson.annotations.SerializedName;/** * 测试使用Gson的@Expose注解和@SerializedName * @author dell * */public class User2 { @Expose(serialize=false) private int id; @Expose(serialize=true) private String uname; @Expose(serialize=true) @SerializedName("年龄") private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public User2(int id, String uname, int age) { super(); this.id = id; this.uname = uname; this.age = age; } public User2() { // TODO Auto-generated constructor stub } } |
测试使用注解转化json字符串:
|
1
2
3
4
5
6
|
public static void obj2JsonDemoWithAnnotation(){ User2 u = new User2(1001, "高淇", 18); Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); System.out.println(gson.toJson(u));} |
结果:
|
1
|
{"uname":"高淇","年龄":18} |
我们发现,age在json字符串中变成了“年龄”,id由于@Expose(serialize=false)没有在json中生成ID相关的内容。

浙公网安备 33010602011771号