加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

JSONObject.toBean中日期格式数据转换失效的解决办法!

发布时间:2020-12-16 19:30:05 所属栏目:百科 来源:网络整理
导读:在Action中我们将JSON格式数据转换为查询BEAN时,通常采用以下方法: JSONObject jsonObject = readJson(request); jsonConfig.setRootClass(User.class); User user=(User)JSONObject.toBean(jsonObject,jsonConfig); 但是转换时会发现,如果传入格式为“20

在Action中我们将JSON格式数据转换为查询BEAN时,通常采用以下方法:

JSONObject jsonObject = readJson(request);

jsonConfig.setRootClass(User.class);

User user=(User)JSONObject.toBean(jsonObject,jsonConfig);

但是转换时会发现,如果传入格式为“2011-01-02”格式的时间参数时

转换后对应的时间参数会变成当前系统时间

查询日志中会发现:Can't transform property 'birthday' from java.lang.String into java.util.Date. Will register a default Morpher

这是因为JSONObject不认识“2011-01-02”格式的时间

这时我们可以采用以下方式解决:

JSONObject jsonObject = readJson(request);

jsonConfig.setRootClass(User.class);

JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[] {"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss"}));

User user=(User)JSONObject.toBean(jsonObject,jsonConfig);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读