JSON 工具类

依赖 

<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>fastjson</artifactId>
   <version>1.2.47</version>
</dependency>

 工具类

 

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> T toBean(String text, Class<T> clazz) {
        return JSON.parseObject(text, clazz);
    }

    /**
     * @param text
     * @return java.lang.Object[]
     * @Description 转换为数组
     * @Author lc
     * @Date 2019/8/2 0002 下午 5:06
     **/
    public static <T> 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 <T> Object[] strToArray(String str, Class<T> clazz) {
        return JSON.parseArray(str, clazz).toArray();
    }

    /**
     * @param str
     * @param clazz
     * @return java.util.List<T>
     * @Description 转换为List
     * @Author lc
     * @Date 2019/8/2 0002 下午 5:04
     **/
    public static <T> List<T> jsonToList(String str, Class<T> 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<K, V>
     * @Description json字符串转化为map
     * @Author lc
     * @Date 2019/8/2 0002 下午 4:57
     **/
    public static <K, V> Map<K, V> strToMap(String str) {
        Map<K, V> map = (Map<K, V>) 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 <K, V> String mapToStr(Map<K, V> map) {
        String str = JSONObject.toJSONString(map);
        return str;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值