原因一:
执行:
JSONArray array = JSONArray.fromObject(this.users);
就会报以下错误:
net.sf.json.JSONException: Java.lang.reflect.InvocationTargetException
users是一个list集合
JSONArray array = JSONArray.fromObject(this.users.toArray());
原因二:
因为bean里有Date字段,且从数据库里读出来的是java.sql.Date赋值给了java.util.Date,转化成JSONArray时出错;可以在从数据库读出Date 时直接写成:new java.util.Date(rs.getDate("date").getTime),这样就不会出错了;
public Date getBrithday() {
System.out.println("brithday:"+brithday);
System.out.println("brithday.getDate():"+brithday.getDate());
System.out.println("brithday.getTime():"+brithday.getTime());
System.out.println(" new java.util.Date(brithday.getTime()):"+new java.util.Date(brithday.getTime()));
return new java.util.Date(brithday.getTime());
//return brithday;
}
输出为
brithday:1989-10-11
brithday.getDate():11
brithday.getTime():624038400000
new java.util.Date(brithday.getTime()):Wed Oct 11 00:00:00 CST 1989
jason格式
["brithday":{"date":11,"day":3,"hours":0,"minutes":0,"month":9,"seconds":0,"time":624038400000,"timezoneOffset":-480,"year":89}]