GsonTool.java
975 Bytes
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
package com.hjx.personalcenter.gson;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class GsonTool {
public GsonTool() {
// TODO Auto-generated constructor stub
}
//使用Gson进行解析Person
public static <T> T getPerson(String jsonString, Class<T> cls) {
T t = null;
try {
Gson gson = new Gson();
t = gson.fromJson(jsonString, cls);
} catch (Exception e) {
// TODO: handle exception
}
return t;
}
// 使用Gson进行解析 List<Person>
public static <T> List<T> getPersons(String jsonString, Class<T> cls) {
List<T> list = new ArrayList<T>();
try {
Gson gson = new Gson();
list = gson.fromJson(jsonString, new TypeToken<List<T>>() {
}.getType());
} catch (Exception e) {
}
return list;
}
}