|
public class Json {
/**
* 对象转换为json字符串
* @param obj
* @return
*/
public String toJson(Object obj) throws UnsupportedEncodingException {
Gson gson = new GsonBuilder()
.setPrettyPrinting()
.disableHtmlEscaping()
.create();
return gson.toJson(obj);
}
/**
* 将Json 转换为对象
* @param json
* @param classOfT
* @param <T>
* @return
*/
public <T> T fromJson(String json, Class<T> classOfT){
Gson gson = new GsonBuilder()
.setPrettyPrinting()
.disableHtmlEscaping()
.create();
Object object = gson.fromJson(json, (Type) classOfT);
return Primitives.wrap(classOfT).cast(object);
}
} |
|