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