import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;
import net.sf.json.util.PropertyFilter;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonUtil {
private static ObjectMapper objectMapper = null;
/**
* JSON初始化
*/
static {
objectMapper = new ObjectMapper();
//设置为中国上海时区
objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
//空值不序列化
objectMapper.setSerializationInclusion(Include.NON_NULL);
//反序列化时,属性不存在的兼容处理
objectMapper.getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
//序列化时,日期的统一格式
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
/**
* 把对象转换成为Json字符串
*
* @param obj
* @return
*/
public static String convertObjectToJson(Object obj) {
if (obj == null) {
// throw new IllegalArgumentException("对象参数不能为空。");
return null;
}
try {
return objectMapper.writeValueAsString(obj);
} catch (IOException e) {
e.printStackTrace();
}
return null;