net.sf.json.JSONException: java.lang.reflect.InvocationTargetException Caused by: java.lang.IllegalArgumentException at java.sql.Date.getHours(Unknown Source)

数据库字段类型为Date,转成JSON格式会有问题,解决方案如下:

json-lib有一个配置类JsonConfig
通过JsonConfig可以注册一个字段处理器
实现JsonValueProcessor接口即可

JsonConfig config = new JsonConfig();
config.registerJsonValueProcessor(java.sql.Date.class, new JsonValueProcessor() {
private SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");

@Override
public Object processArrayValue(Object arg0, JsonConfig arg1) {
return null;
}

@Override
public Object processObjectValue(String arg0, Object arg1, JsonConfig arg2) {
return arg1 == null ? "" : sd.format(arg1);
}
});
JSONArray jsonArray = JSONArray.fromObject(articles, config);// 记得把config放进去

 

posted @ 2017-04-07 21:47  鄒成立  阅读(1376)  评论(0编辑  收藏  举报