JSON 工具类

依赖 


   com.alibaba
   fastjson
   1.2.47

 工具类

 

package com.netty.server.netty.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.JSONLibDataFormatSerializer;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.util.List;
import java.util.Map;

/**
 * @Description fastJson工具类
 * @Author lc
 * @Date 2019/8/2 0002 下午 5:07
 */
public class FastJsonUtils {

    private static final SerializeConfig config;

    static {
        config = new SerializeConfig();
        // 使用和json-lib兼容的日期输出格式
        config.put(java.util.Date.class, new JSONLibDataFormatSerializer());
        // 使用和json-lib兼容的日期输出格式
        config.put(java.sql.Date.class, new JSONLibDataFormatSerializer());
    }

    private static final SerializerFeature[] features = {
            // 输出空置字段
            SerializerFeature.WriteMapNullValue,
            // list字段如果为null,输出为[],而不是null
            SerializerFeature.WriteNullListAsEmpty,
            // 数值字段如果为null,输出为0,而不是null
            SerializerFeature.WriteNullNumberAsZero,
            // Boolean字段如果为null,输出为false,而不是null
            SerializerFeature.WriteNullBooleanAsFalse,
            // 字符类型字段如果为null,输出为"",而不是null
            SerializerFeature.WriteNullStringAsEmpty
    };


    public static String convertObjectToJSON(Object object) {
        return JSON.toJSONString(object, config, features);
    }

    public static String toJSONNoFeatures(Object object) {
        return JSON.toJSONString(object, config);
    }

    /**
     * @param str
     * @return java.lang.Object
     * @Description json转对象
     * @Author lc
     * @Date 2019/8/2 0002 下午 5:05
     **/
    public static Object jsonToBean(String str) {
        return JSON.parse(str);
    }

    /**
     * @param text
     * @param clazz
     * @return T
     * @Description json转对象
     * @Author lc
     * @Date 2019/8/2 0002 下午 5:06
     **/
    public static  T toBean(String text, Class clazz) {
        return JSON.parseObject(text, clazz);
    }

    /**
     * @param text
     * @return java.lang.Object[]
     * @Description 转换为数组
     * @Author lc
     * @Date 2019/8/2 0002 下午 5:06
     **/
    public static  Object[] toArray(String text) {
        return toArray(text);
    }

    /**
     * @param str
     * @param clazz
     * @return java.lang.Object[]
     * @Description 转换为数组
     * @Author lc
     * @Date 2019/8/2 0002 下午 5:04
     **/
    public static  Object[] strToArray(String str, Class clazz) {
        return JSON.parseArray(str, clazz).toArray();
    }

    /**
     * @param str
     * @param clazz
     * @return java.util.List
     * @Description 转换为List
     * @Author lc
     * @Date 2019/8/2 0002 下午 5:04
     **/
    public static  List jsonToList(String str, Class clazz) {
        return JSON.parseArray(str, clazz);
    }

    /**
     * @param str
     * @return java.lang.Object
     * @Description 将string转化为序列化的json字符串
     * @Author lc
     * @Date 2019/8/2 0002 下午 4:48
     **/
    public static Object strToJson(String str) {
        Object objectJson = JSON.parse(str);
        return objectJson;
    }

    /**
     * @param str
     * @return java.util.Map
     * @Description json字符串转化为map
     * @Author lc
     * @Date 2019/8/2 0002 下午 4:57
     **/
    public static  Map strToMap(String str) {
        Map map = (Map) JSONObject.parseObject(str);
        return map;
    }

    /**
     * @param jsonStr
     * @param clazz
     * @return java.lang.Object
     * @Description 转换JSON字符串为对象
     * @Author lc
     * @Date 2019/8/2 0002 下午 4:58
     **/
    public static Object jsonToObject(String jsonStr, Class clazz) {
        return JSONObject.parseObject(jsonStr, clazz);
    }

    /**
     * @param map
     * @return java.lang.String
     * @Description 将map转化为string
     * @Author lc
     * @Date 2019/8/2 0002 下午 4:59
     **/
    public static  String mapToStr(Map map) {
        String str = JSONObject.toJSONString(map);
        return str;
    }
}

你可能感兴趣的:(java,json,json工具类)