java设置Date的json输出格式的问题
本帖最后由 JiangYouJiShiYou 于 2013-01-24 12:03:15 编辑 为什么我照着网上的方法用JsonValueProcessor来自定义Date的输入格式,但是得到的依然是以下的json格式数据呢:{"time":1358973725447,"minutes":42,"seconds":5,"hours":4,"month":0,"year":113,"timezoneOffset":-480,"day":4,"date":24} 我想得到的是"yyyy-MM-dd'T'HH:mm:ss"格式的日期数据,应该怎么办?
连processObjectValue中的System.out.println都没有运行,到底是什么问题啊(我用的是SSH架构,前面那一段代码是写在某个action里面的),会不会是其他什么地方出了问题?求解
核心代码:
JsonConfig config = new JsonConfig();
config.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor());
JSONObject jsonObject = JSONObject.fromObject(new Date(),config);
response.getWriter().write(jsonObject.toString());
import java.text.SimpleDateFormat;
import java.util.Date;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
public class DateJsonValueProcessor implements JsonValueProcessor {
private String format = "yyyy-MM-dd'T'HH:mm:ss";
public DateJsonValueProcessor()
{
System.out.println("DateJsonValueProcessor");
}
public DateJsonValueProcessor(String format)
{
System.out.println("DateJsonValueProcessor(String format)");
this.format = format;
}
@Override
public Object processArrayValue(Object value, JsonConfig jsonConfig)
{
System.out.println("processArrayValue");
String[] obj = {};
if (value instanceof Date[])
{
SimpleDateFormat sf = new SimpleDateFormat(format);
Date[] dates = (Date[]) value;
obj = new String[dates.length];
for (int i = 0; i < dates.length; i++)
{
obj[i] = sf.format(dates[i]);
}
}
return obj;
}
@Override
public Object processObjectValue(String key, Object value, JsonConfig jsonConfig)
{
System.out.println("processObjectValue");
if (value instanceof Date)
{
String str = new SimpleDateFormat(format).format((Date) value);
return str;
}
return value;
}
public String getFormat()
{
return format;
}
public void setFormat(String format)
{
this.format = format;
}
}
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)
*/
public class JsonDateValueProcessor implements JsonValueProcessor {
/**
* datePattern
*/
private String datePattern = "yyyy-MM-dd";
public JsonDateValueProcessor() {
super();
}
/**
* 可以根据构造传入格式化的格式
*/
public JsonDateValueProcessor(String format) {
super();
if(format != null && !"".equals(format)){
this.datePattern = format;
}
}
@Override
public Object processArrayValue(Object value, JsonConfig jsonConfig) {
return process(value);
}
@Override
public Object processObjectValue(String key, Object value,
JsonConfig jsonConfig) {
return process(value);
}
//用于格式化date类型的值
private Object process(Object value) {
try {
if (value instanceof Date) {
SimpleDateFormat sdf = new SimpleDateFormat(datePattern,
Locale.CHINESE);
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;
}
}