首页 > Web开发 > 详细

json 帮助工具

时间:2015-12-21 10:34:19      阅读:148      评论:0      收藏:0      [点我收藏+]

import java.lang.reflect.Type;

import com.google.gson.Gson;

/**
 * json 帮助工具
 */
public final class GsonUtil {

    private GsonUtil() {

    }

    /**
     * Object转JSON对象
     *
     * @param obj
     * @return
     */
    public static String toJson(Object object) {
        String json = null;
        if (object != null) {
            Gson gson = new Gson();
            json = gson.toJson(object);
        }
        return json;
    }

    /**
     * 字符串转java对象
     *
     * @param str
     * @param clazz
     * @return
     */
    public static <T> T fromJson(String json, Class<T> clazz) {
        T t = null;
        if (json != null) {
            Gson gson = new Gson();
            t = gson.fromJson(json, clazz);
        }
        return t;
    }

    /**
     * 字符串转java对象
     * @param json
     * @param type
     * @return
     */
    public static <T> T fromJson(String json, Type type) {
        T t = null;
        if (json != null) {
            Gson gson = new Gson();
            t = gson.fromJson(json, type);
        }
        return t;
    }
}

json 帮助工具

原文:http://www.cnblogs.com/yanduanduan/p/5062497.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!