# FastJson封装的工具类

FastJson封装的工具类


/**
 * 基于FastJson封装JSON
 */
public class JsonUtils {
    private static SerializeConfig config;
    private static SerializerFeature[] features = {
            //输出空值字段
            SerializerFeature.WriteMapNullValue,
            //如果数组结果为null,则输出为[],而不是null
            SerializerFeature.WriteNullListAsEmpty,
            //数值字段为null,则输出为0,而不是null
            SerializerFeature.WriteNullNumberAsZero,
            //Boolean字段为null,则输出为false,而不是null
            SerializerFeature.WriteNullBooleanAsFalse,
            //字符类型如果为null,则输出为" ",而不是null
            SerializerFeature.WriteNullStringAsEmpty
    };
    static {
        config = new SerializeConfig();
        //使用json-lib兼容的日期输出格式
        config.put(java.util.Date.class,new JSONLibDataFormatSerializer());
    }

    /**
     * 将一个对象装换为Json字符串
     */
    public static String toJSONString(Object object) {
        return JSONObject.toJSONString(object,config,features);
    }

    /**
     * 将Json字符串转换为Object类型的
     * */
    public  static  Object toObject(String str){
        return JSON.parse(str);
    }

    /**
     * 将Json字符串转换为实例
     * */
    public static <T> T toObject(String str,Class<T> t){
        return JSON.parseObject(str,t);
    }

    /**
     * 将Json转换为指定类型的List
     * */
    public static <T> List<T> toList(String str, Class<T> t){
        return JSON.parseArray(str,t);
    }

    /**
     * 将Json转换为Map
     * */
    public static <T> Map<String,T> toMap(String str){
        return (Map<String, T>) JSONObject.parseObject(str);
    }
}

发布了123 篇原创文章 · 获赞 9 · 访问量 3982

猜你喜欢

转载自blog.csdn.net/qq_37248504/article/details/103774298