List转jsonArray :
// 格式化日期 JsonConfig jsonConfig = new JsonConfig(); DSHJsonDateValueProcessor dshJsonDateValueProcessor = new DSHJsonDateValueProcessor(); jsonConfig.registerJsonValueProcessor(Timestamp.class, dshJsonDateValueProcessor); JSONArray jsonArray = JSONArray.fromObject(appUserGroupList, jsonConfig);
DSHJsonDateValueProcessor:
public class DSHJsonDateValueProcessor implements JsonValueProcessor { public Object processArrayValue(Object value, JsonConfig jsonConfig) { return process(value); } public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) { return process(value); } private Object process(Object value){ if(value instanceof Date){ SimpleDateFormat sdf = new SimpleDateFormat(DSHUtils.DATEFORMAT_YYYY_MM_DD_24HH_MM_SS); return sdf.format(value); } else if (value instanceof Timestamp) { SimpleDateFormat sdf = new SimpleDateFormat(DSHUtils.DATEFORMAT_YYYY_MM_DD_24HH_MM_SS); return sdf.format(value); } return value == null ? "" : value.toString(); } }
jsonArray转List:
JSONArray array = JSONArray.fromObject(appUserGroupListStr); JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[] { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss" }), true); List<AppUserVO> appUserGroupList = (List<AppUserVO>) JSONArray.toList(array, new AppUserVO(), new JsonConfig());
注:VO对象里面日期属性不能用java.sql.Timestamp,得用java.util.Date