Map的存储结构式Key/Value形式,Key 和 Value可以是普通类型,也可以是自己写的JavaBean(本文),还可以是带有泛型的List.
(GSON的GitHub项目页:https://github.com/google/gson)
JavaBean
本例中您要重点看如何将Json转回为普通JavaBean对象时TypeToken的定义.
实体类:
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
|
public class Point {
private int x;
private int y;
public Point( int x, int y) {
this .x = x;
this .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;
}
@Override
public String toString() {
return "Point [x=" + x + ", y=" + y + "]" ;
}
}
|
测试类:
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
|
import java.util.LinkedHashMap;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
public class GsonTest3 {
public static void main(String[] args) {
Gson gson = new GsonBuilder().enableComplexMapKeySerialization()
.create();
Map<Point, String> map1 = new LinkedHashMap<Point, String>(); // 使用LinkedHashMap将结果按先进先出顺序排列
map1.put( new Point( 5 , 6 ), "a" );
map1.put( new Point( 8 , 8 ), "b" );
String s = gson.toJson(map1);
System.out.println(s); // 结果:[[{"x":5,"y":6},"a"],[{"x":8,"y":8},"b"]]
Map<Point, String> retMap = gson.fromJson(s,
new TypeToken<Map<Point, String>>() {
}.getType());
for (Point p : retMap.keySet()) {
System.out.println( "key:" + p + " values:" + retMap.get(p));
}
System.out.println(retMap);
System.out.println( "----------------------------------" );
Map<String, Point> map2 = new LinkedHashMap<String, Point>();
map2.put( "a" , new Point( 3 , 4 ));
map2.put( "b" , new Point( 5 , 6 ));
String s2 = gson.toJson(map2);
System.out.println(s2);
Map<String, Point> retMap2 = gson.fromJson(s2,
new TypeToken<Map<String, Point>>() {
}.getType());
for (String key : retMap2.keySet()) {
System.out.println( "key:" + key + " values:" + retMap2.get(key));
}
}
}
|
结果:
1
2
3
4
5
6
7
8
|
[[{"x":5,"y":6},"a"],[{"x":8,"y":8},"b"]]
key:Point [x=5, y=6] values:a
key:Point [x=8, y=8] values:b
{Point [x=5, y=6]=a, Point [x=8, y=8]=b}
----------------------------------
{"a":{"x":3,"y":4},"b":{"x":5,"y":6}}
key:a values:Point [x=3, y=4]
key:b values:Point [x=5, y=6]
|
泛型List
实体类:
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
import java.util.Date;
public class Student {
private int id;
private String name;
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 + "]" ;
}
}
public class Teacher {
private int id;
private String name;
private String title;
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 String getTitle() {
return title;
}
public void setTitle(String title) {
this .title = title;
}
@Override
public String toString() {
return "Teacher [id=" + id + ", name=" + name + ", id="codetool">
测试类:
输出结果:
|