fastjson强转datatime为时间戳解决方案

fastjson输出json对象时,会将时间格式自动转换成时间戳,网上提供的思路

1.重写json实现类
2.在实体类对应字段加上注解@JSONField(format = "yyyy-MM-dd HH:mm:ss") //FastJson包使用注解
3.JSON.toJSONStringWithDateFormat(data, "yyyy-MM-dd HH:mm:ss")

我这边用的时map所以不存在实体类一说,用第三种方式可以解决问题,但第三种方式的字符串不符合json输出,于是再加一层转换,解决问题

@RequestMapping("activityListApi.htm")
    @ResponseBody
    public JSONObject activityListApi() {
        JSONObject jsonObject = new JSONObject();
        HashMap<String, String> map = new HashMap<>();
        List<Map> list = miniProgramActivityService.getMiniProgramActivityList(map);
        jsonObject.put("code", 1);
        jsonObject.put("data", JSON.parse(JSONObject.toJSONStringWithDateFormat(list,"yyyy-MM-dd HH:mm:ss.SSS")));
        jsonObject.put("message", "活动列表请求成功!");
        return jsonObject;
    }

上面的结果如下

{
    "code": 1,
    "data": [
        {
            "start_time": "2019-06-01 00:00:00.000",
            "icon_img": "http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoQYiaktFqPGp1IChYYQzWONZODQ2s3AKu6vDJ1gyPcT6m2StZOylpOBPQDdrx2LbxSAoibmvWnTgeQ/132",
            "create_time": "2019-06-04 03:46:53.000",
            "name": "优惠券活动",
            "end_time": "2019-06-30 00:00:00.000",
            "bg_img": "http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoQYiaktFqPGp1IChYYQzWONZODQ2s3AKu6vDJ1gyPcT6m2StZOylpOBPQDdrx2LbxSAoibmvWnTgeQ/132",
            "id": 1,
            "sort": 1,
            "desc": "优惠券活动,没人限领两张",
            "status": 1
        }
    ],
    "message": "活动列表请求成功!"
}

转载于:https://www.jianshu.com/p/3990d5d283c4

猜你喜欢

转载自blog.csdn.net/weixin_33777877/article/details/91215934