Json序列化 解决Date问题

来源:互联网 发布:最新聊天软件排行 编辑:程序博客网 时间:2024/06/05 05:05
List<SoftwareDownloadCount> softWares = softWareService.softWareList(user);
JSONObject json = new JSONObject();
 
JsonConfig config = new JsonConfig();   
config.setIgnoreDefaultExcludes(false);      
config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);    
config.registerJsonValueProcessor(Date.class,new JsonDateValueProcessor(JsonDateValueProcessor.TIME_FORMAT)); //date processor register 
JSONArray joson = JSONArray.fromObject(softWares,config);  

return ajaxJson(joson.toString());


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;


import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;


/** 
 * JSON 日期格式处理(java转化为JSON) 
 * @author sailor 
 */  
public class JsonDateValueProcessor implements JsonValueProcessor {  
  
    /** 
     * datePattern 
     */  
     private String datePattern = "yyyy-MM-dd";  
// 长日期格式
public static String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
// 长日期格式 精确到毫秒
public static String LONG_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss SSS";
  
    /** 
     * JsonDateValueProcessor 
     */  
    public JsonDateValueProcessor() {  
        super();  
    }  
  
    /** 
     * @param format 
     */  
    public JsonDateValueProcessor(String format) {  
        super();  
        this.datePattern = format;  
    }  
  
    /** 
     * @param value 
     * @param jsonConfig 
     * @return Object 
     */  
    public Object processArrayValue(Object value, JsonConfig jsonConfig) {  
        return process(value);  
    }  
  
    /** 
     * @param key 
     * @param value 
     * @param jsonConfig 
     * @return Object 
     */  
    public Object processObjectValue(String key, Object value,  
            JsonConfig jsonConfig) {  
        return process(value);  
    }  
  
    /** 
     * process 
     * @param value 
     * @return 
     */  
    private Object process(Object value) {  
        try {  
            if (value instanceof Date) {  
                SimpleDateFormat sdf = new SimpleDateFormat(datePattern,  
                        Locale.UK);  
                return sdf.format((Date) value);  
            }  
            return value == null ? "" : value.toString();  
        } catch (Exception e) {  
            return "";  
        }  
  
    }  
  
    /** 
     * @return the datePattern 
     */  
    public String getDatePattern() {  
        return datePattern;  
    }  
  
    /** 
     * @param pDatePattern the datePattern to set 
     */  
    public void setDatePattern(String pDatePattern) {  
        datePattern = pDatePattern;  
    }