net.sf.json.JSONObject转Bean对象时常见报错问题

JSONObject到Bean转换问题及解决方案

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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值