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