一、概述
public class FastJsonUtils {
private static final SerializeConfig config;
static {
config = new SerializeConfig();
config.put(java.util.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式
config.put(java.sql.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式
}
/**
* 输出json字符串的属性要求,如果方法中未加入此属性则json格式按照默认的格式输出
*/
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,
// 禁止循环引用
SerializerFeature.DisableCircularReferenceDetect,
};
/**
*
* @Function: FastJsonUtils.java
* @Description: object转json
* @version: v1.0.0
* @date: 2018年9月11日 上午9:12:53
*/
public static String objectToJSON(Object obj) {
return JSON.toJSONString(obj, config, features);
}
/**
*
* @Function: FastJsonUtils.java
* @Description: object转json不带Features
* @version: v1.0.0
* @date: 2018年9月11日 上午9:13:22
*/
public static String toJSONNoFeatures(Object obj) {
return JSON.toJSONString(obj, config);
}
/**
*
* @Function: FastJsonUtils.java
* @Description: json转object
* @version: v1.0.0
* @date: 2018年9月11日 上午9:12:25
*/
public static Object stringToBean(String text) {
return JSON.parse(text);
}
/**
* json转泛类
* @param text
* @param clazz
* @param <T>
* @return
*/
public static <T> T stringToBean(String text, Class<T> clazz) {
return JSON.parseObject(text, clazz);
}
/**
*
* @Function: FastJsonUtils.java
* @Description: json转object数组
* @version: v1.0.0
* @date: 2018年9月11日 上午9:15:57
*/
public static <T> Object[] toArray(String text) {
return toArray(text, null);
}
/**
*
* @Function: FastJsonUtils.java
* @Description: json转泛数组
* @version: v1.0.0
* @date: 2018年9月11日 上午9:15:52
*/
public static <T> Object[] toArray(String text, Class<T> clazz) {
return JSON.parseArray(text, clazz).toArray();
}
/**
*
* @Function: FastJsonUtils.java
* @Description: json转list
* @version: v1.0.0
* @date: 2018年9月11日 上午9:15:47
*/
public static <T> List<T> toList(String text, Class<T> clazz) {
return JSON.parseArray(text, clazz);
}
/**
*
* @Function: FastJsonUtils.java
* @Description: 字符串转map
* @version: v1.0.0
* @date: 2018年9月11日 上午9:17:19
*/
public static <K, V> Map<K, V> stringToCollect(String s) {
Map<K, V> m = (Map<K, V>) JSONObject.parseObject(s);
return m;
}
/**
*
* @Function: FastJsonUtils.java
* @Description: 该函数的功能描述
* @version: v1.0.0
* @date: 2018年9月11日 上午9:18:06
*/
public static Object convertJsonToObject(String jsonData, Class<?> clazz) {
return JSONObject.parseObject(jsonData, clazz);
}
public static Object convertJSONToObject(String content, Class<?> clazz) {
return JSONObject.parseObject(content, clazz);
}
/**
*
* @Function: FastJsonUtils.java
* @Description: 该函数的功能描述
* @version: v1.0.0
* @date: 2018年9月11日 上午9:18:47
*/
public static <K, V> String collectToString(Map<K, V> m) {
String s = JSONObject.toJSONString(m);
return s;
}
public static <T> T stringToBean(String content, TypeReference<T> typeReference) {
return JSONObject.parseObject(content, typeReference);
}
public static String getString(String json,String key){
return JSON.parseObject(json).getString(key);
}
}