fastjson工具类
package util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.JSONLibDataFormatSerializer;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.sun.org.apache.xml.internal.security.keys.content.KeyValue;
import java.util.List;
import java.util.Map;
/**
* @Author: REN
* @Description:
* @Date: Created in 1:46 2018/3/14
*/
public class JsonUtil {
private static final SerializeConfig config;
static {
config = new SerializeConfig();
config.put(java.util.Date.class, new JSONLibDataFormatSerializer());
config.put(java.sql.Date.class, new JSONLibDataFormatSerializer());
}
private static final SerializerFeature[] features = {SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullListAsEmpty,
SerializerFeature.WriteNullNumberAsZero,
SerializerFeature.WriteNullBooleanAsFalse,
SerializerFeature.WriteNullStringAsEmpty
};
public static String toJSONString(Object object) {
return JSON.toJSONString(object, config, features);
}
public static String toJSONNoFeatures(Object object) {
return JSON.toJSONString(object, config);
}
public static Object toBean(String text) {
return JSON.parse(text);
}
public static <T> T toBean(String text, Class<T> clazz) {
return JSON.parseObject(text, clazz);
}
public static <T> Object[] toArray(String text) {
return toArray(text, null);
}
public static <T> Object[] toArray(String text, Class<T> clazz) {
return JSON.parseArray(text, clazz).toArray();
}
public static <T> List<T> toList(String text, Class<T> clazz) {
return JSON.parseArray(text, clazz);
}
/**
* 将javabean转化为序列化的json字符串
* @param keyvalue
* @return
*/
public static Object beanToJson(KeyValue keyvalue) {
String textJson = JSON.toJSONString(keyvalue);
Object objectJson = JSON.parse(textJson);
return objectJson;
}
/**
* 将string转化为序列化的json字符串
* @param keyvalue
* @return
*/
public static Object textToJson(String text) {
Object objectJson = JSON.parse(text);
return objectJson;
}
/**
* json字符串转化为map
* @param s
* @return
*/
public static Map stringToCollect(String s) {
Map m = JSONObject.parseObject(s);
return m;
}
/**
* 将map转化为string
* @param m
* @return
*/
public static String collectToString(Map m) {
String s = JSONObject.toJSONString(m);
return s;
}
public static String listToJsonArray(List list){
String s = JSONArray.toJSONString(list);
return s;
}
}