对象转换为JSON字符串时,造成日期数据的格式不正确问题-JSONObject.toJSONString
json
适用于现代 C++ 的 JSON。
项目地址:https://gitcode.com/gh_mirrors/js/json
免费下载资源
·
数据库存储的数据如下,格式为yyyy-MM-dd hh:mm:ss
封装实体类后,日期数据格式为:
birthday=Tue Feb 27 17:47:08 CST 2018
可以看出,日期格式没有问题,但是在经过该代码后,日期数据格式被jackson自动替换了
String s = JSONObject.toJSONString(pojo);
//pojo是接收的实体类对象
结果为:
"birthday":1519724828000
解决方法
toJSONString方法为我们提供了一个参数:
SerializerFeature.WriteDateUseDateFormat
改造上面的代码为:
String s = JSONObject.toJSONString(pojo, SerializerFeature.WriteDateUseDateFormat);
SerializerFeature.WriteDateUseDateFormat默认的日期格式为yyyy-MM-dd hh:mm:ss
再次执行,日期格式正确
"birthday":"2018-02-27 17:47:08"
GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:21 天前 )
960b763e
3 个月前
8c391e04
6 个月前
更多推荐
已为社区贡献15条内容
所有评论(0)