精易论坛

标题: Gson JSON转换 [打印本页]

作者: hnzrb    时间: 2018-7-21 20:58
标题: Gson JSON转换
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);
    }
}
作者: longsui48    时间: 2018-7-21 22:28
java的代码 试试看怎么转e
作者: mainshentong    时间: 2018-8-16 01:59
xxxxxxxxxxxxxxxxxxx




欢迎光临 精易论坛 (https://125.confly.eu.org/) Powered by Discuz! X3.4