日期:2014-05-17 浏览次数:20813 次
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,&