zoukankan      html  css  js  c++  java
  • json 帮助工具

    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;
        }
    }

  • 相关阅读:
    BZOJ 3529 数表
    BZOJ 3832 Rally
    BZOJ 1086 王室联邦
    BZOJ 2738 矩阵乘法
    2656565
    小L的区间求和
    小L的直线
    Co-prime 杭电4135
    POJ 跳蚤
    B
  • 原文地址:https://www.cnblogs.com/yanduanduan/p/5062497.html
Copyright © 2011-2022 走看看