public class GsonUtils {
public static String createGsonString(Object object) {
Gson gson = new Gson();
String gsonString = gson.toJson(object);
return gsonString;
}
public static <T> T changeGsonToBean(String gsonString, Class<T> cls) {
Gson gson = new Gson();
T t = gson.fromJson(gsonString, cls);
return t;
}
public static <T> List<T> changeGsonToList(String gsonString, Class<T> cls) {
Gson gson = new Gson();
List<T> list = gson.fromJson(gsonString, new TypeToken<List<T>>() {
}.getType());
return list;
}
public static <T> List<Map<String, T>> changeGsonToListMaps(
String gsonString) {
List<Map<String, T>> list = null;
Gson gson = new Gson();
list = gson.fromJson(gsonString, new TypeToken<List<Map<String, T>>>() {
}.getType());
return list;
}
public static <T> Map<String, T> changeGsonToMaps(String gsonString) {
Map<String, T> map = null;
Gson gson = new Gson();
map = gson.fromJson(gsonString, new TypeToken<Map<String, T>>() {
}.getType());
return map;
}
}
网友评论