net.sf.json.JSONObject转Bean对象时常见报错问题
错误1:net.sf.json.JSONException: Error while setting property=starttime type class java.lang.String
因为对象里面包含时间类型 Timestamp 或者 Date
解决办法:
// 在转为对象前先 String -- > Timestamp Date
JSONUtils.getMorpherRegistry().registerMorpher(new TimestampMorpher(new String[] {"yyyy-MM-dd HH:mm:ss","yyyy-MM-dd"}));
AttendanceInfo attendanceInfo = (AttendanceInfo) JSONObject.toBean(object,AttendanceInfo.class);
错误2: Could not write JSON: No serializer found for class net.sf.ezmorph.MorpherRegistry and no properties discovered to create BeanSerializer
因为对象里面包含数组类型
解决办法:
// 在Map中put你要转换的类中的集合名,然后在toBean()的时候把参数加上
Map classMap = new HashMap();
classMap.put("points",VPoint.class);
AttendanceInfo attendanceInfo = (AttendanceInfo) JSONObject.toBean(object,AttendanceInfo.class,classMap);
JSONObject到Bean转换问题及解决方案
922

被折叠的 条评论
为什么被折叠?



