// 格式化日期
JsonConfig jsonConfig = new JsonConfig();
JsonDateValueProcessor dshJsonDateValueProcessor = new JsonDateValueProcessor();
jsonConfig.registerJsonValueProcessor(Timestamp.class, dshJsonDateValueProcessor);
JSONArray jsonArray = JSONArray.fromObject(List, jsonConfig);
另建一个类
JsonDateValueProcessor
package com.jh.Admin.controller;
import java.sql.Date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
public class JsonDateValueProcessor 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("yyyy-MM-dd HH:mm:ss");
return sdf.format(value);
} else if (value instanceof Timestamp) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(value);
}
return value == null ? "" : value.toString();
}
}
///////////////////////////////////////////////////////////////////////////////////
jsonArray转List:
JSONArray array = JSONArray.fromObject(xxxList);
JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[] { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss" }), true);
List<xxxx> appUserGroupList = (List<xxxx>) JSONArray.toList(array, new xxx, new JsonConfig());