今天使用JSON,格式往前台传递数据,老是报这个错误,自己也太笨,检查了好几遍,也不知道是哪里的问题,上网上一艘,原来是因为json并不能处理数据库中data字段的属性值,应为data的精度不够,使用json的时候,需要注册日期转换工具,
我的错误就如图所示(简写),
QueryRunner qr = new QueryRunner(DataSourceUtil.getDataSource());
String sql = "SELECT * FROM product LIMIT 8";
List<Goods> list = qr.query(sql, new BeanListHandler<>(Goods.class));
//将list集合转化为JSOn
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println(jsonArray.toString())
报错:
net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
Caused by: java.lang.IllegalArgumentException
at java.sql.Date.getHours(Date.java:187)
... 50 more
可以指定,不转化这个date数据类型,跳过它
JSONConfig 对象,有这个功能
QueryRunner qr = new QueryRunner(DataSourceUtil.getDataSource());
String sql = "SELECT * FROM product LIMIT 8";
List<Goods> list = qr.query(sql, new BeanListHandler<>(Goods.class));
//将list集合转化为JSOn
JsonConfig js = new JsonConfig();
js.setExcludes(new String[]{"pdate"});
JSONArray jsonArray = JSONArray.fromObject(list,js);
System.out.println(jsonArray.toString());
这样就好了
```