Json转换工具类FastJsonUtil

2 篇文章 0 订阅

pom.xml

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

FastJsonUtil工具类

package com.common.util;

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

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

/**
 * @author: 
 * @create_time: 2018-03-28 13:39
 * @describe: fast json 工具
 */
public final class FastJsonUtil {
    private FastJsonUtil() {

    }

    /**
     * 将对象转换为json
     * null ->list =[]
     * 输出null 值
     * @param value 对象
     * @return
     */
    public static String obj2json(Object value) {
        try {
            return JSON.toJSONString(value,SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullListAsEmpty);
        } catch (Exception e) {
            try {
                return obj2json3(value);
            } catch (Exception e1) {
                e.printStackTrace();
                return null;
            }
        }
    }

    public static String obj2jsonPretty(Object value) {
        try {
            return JSON.toJSONString(value, SerializerFeature.PrettyFormat);
        } catch (Exception e) {
            try {
                return obj2json2(value);
            } catch (Exception e1) {
                e.printStackTrace();
                return null;
            }
        }
    }

    public static String obj2jsonDateFormat(Object value) {
        try {
            return JSON.toJSONStringWithDateFormat(value, "yyyy-MM-dd HH:mm:ss",
                    SerializerFeature.WriteDateUseDateFormat,SerializerFeature.DisableCircularReferenceDetect);
        } catch (Exception e) {
            try {
                return obj2json2(value);
            } catch (Exception e1) {
                e.printStackTrace();
                return null;
            }
        }
    }

    public static String obj2jsonDateFormat(Object value, String format) {
        try {
//			return JSON.toJSONString(value, SerializerFeature.PrettyFormat);
            return JSON.toJSONStringWithDateFormat(value, format,
                    SerializerFeature.WriteDateUseDateFormat);
        } catch (Exception e) {
            try {
                return obj2json2(value);
            } catch (Exception e1) {
                e.printStackTrace();
                return null;
            }
        }
    }

    @SuppressWarnings("deprecation")
    public static String obj2json2(Object value) {
        try {
            return JSON.toJSONString(value, SerializerFeature.WriteTabAsSpecial, SerializerFeature.WriteSlashAsSpecial);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @SuppressWarnings("deprecation")
    public static String obj2json3(Object value) {
        try {
            return JSON.toJSONString(value, SerializerFeature.WriteTabAsSpecial, SerializerFeature.WriteSlashAsSpecial,
                    SerializerFeature.BrowserCompatible);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 将json转换为对象
     *
     * @param <T>   转换的类型
     * @param json  json数据
     * @param clazz 被转化对象的class
     * @return
     */
    public static <T> T json2obj(String json, Class<T> clazz) {
        try {
            return JSON.parseObject(json, clazz);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 将json转换为对象数组
     *
     * @param <T>   转换的类型
     * @param json  json数据
     * @param clazz 被转化对象的class
     * @return
     */
    public static <T> List<T> json2List(String json, Class<T> clazz) {
        try {
            return JSON.parseArray(json, clazz);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * jsonStr to Map
     *
     * @param jsonStr jsonStr
     * @return
     * @author tongys
     * @date 2013-4-2
     */
    public static Map<String, String> parseJSON2Map(String jsonStr) {

        // 参数di
        Map<String, String> map = new HashMap<String, String>();
        JSONObject json = null;

        // 最外层解析
        try {
            json = JSONObject.parseObject(jsonStr);
            for (String k : json.keySet()) {
                Object v = json.get(k);
                if (null != v) {
                    map.put(k.toString(), v.toString());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return map;
    }

    /**
     * jsonStr to Map
     *
     * @param jsonStr jsonStr
     * @return
     */
    public static Map<String, Object> parseJSON2Map2(String jsonStr) {

        // 参数di
        Map<String, Object> map = new HashMap<String, Object>();
        JSONObject json = null;

        // 最外层解析
        try {
            json = JSONObject.parseObject(jsonStr);
            for (String k : json.keySet()) {
                Object v = json.get(k);
                if (null != v) {
                    map.put(k.toString(), v.toString());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return map;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值