解决JsonArray.toList转换List时,串中的时间字段变为当前时间的问题

当前问题:
最近在处理一个Bug时发现从数据库中取出的时间,到页面后全部变为了当前时间.跟程序走完后发现,在服务中取出库中内容后,服务用
String result = JSON.toJSONString(list);
将list变为串传到前端页面,这时时间格式变成毫秒制,前端页面在使用
JSONArray jsonList = JSONArray.fromObject(jsonObject.getString(“list”));
list = (List)JSONArray.toList(jsonList,Acceptorder.class);
将从服务传递而来的串重新转换为list;可是在转换过程中,无法完美的将毫秒重新变为日期格式,导致时间变为当前时间;
问题原因:
尝试许久发现是jar包问题,导致时间转换为串后无法转变为日期格式;
解决方法:
在将list转换为串使用
String result = JSON.toJSONStringWithDateFormat(list,“yyyy-MM-dd HH:mm:ss”);
在将串转换回list时 使用
JSONObject jsonObject = JSONObject.fromObject(strjson);
JSONArray jsonList = JSONArray.fromObject(jsonObject.getString(“list”));
//处理日期
JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[] { “yyyy-MM-dd”, “yyyy-MM-dd HH:mm:ss” }), true);
list = (List)JSONArray.toList(jsonList,Acceptorder.class);

猜你喜欢

转载自blog.csdn.net/HuanJun_/article/details/83418213