json与fastjson
1、若是对象中包含date类型,那么利用,将日期转成String,因为date直接转成date会错误,或者利用fastjson的JSON.toJSONString(dg),返回毫秒数
public class JsonDateValueProcessor implements JsonValueProcessor{ private String pattern = "yyyy-MM-dd HH:mm:ss"; public Object processArrayValue(Object value, JsonConfig config) { return process(value); } public Object processObjectValue(String key, Object value, JsonConfig config) { return process(value); } private Object process(Object value){ if(value instanceof Date){ SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.CHINA); return sdf.format(value); } return value == null ? "" : value.toString(); } }
JsonConfig config = new JsonConfig(); JsonDateValueProcessor jsonValueProcessor = new JsonDateValueProcessor(); config.registerJsonValueProcessor(Date.class, jsonValueProcessor)
JSONObject json=JSONObject.fromObject(dg, config)