JsonUtil.java
1017 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
41
42
43
44
45
46
package com.webank.utils;
import java.lang.reflect.Type;
import com.google.gson.Gson;
/**
* Java对象和JSON字符串相互转化工具类
* @author penghuaiyi
* @date 2013-08-10
*/
public final class JsonUtil {
public JsonUtil(){}
/**
* 对象转换成json字符串
* @param obj
* @return
*/
public static String toJson(Object obj) {
Gson gson = new Gson();
return gson.toJson(obj);
}
/**
* json字符串转成对象
* @param str
* @param type
* @return
*/
public static <T> T fromJson(String str, Type type) {
Gson gson = new Gson();
return gson.fromJson(str, type);
}
/**
* json字符串转成对象
* @param str
* @param type
* @return
*/
public static <T> T fromJson(String str, Class<T> type) {
Gson gson = new Gson();
return gson.fromJson(str, type);
}
}