本文介绍了日期格式映射到JSON Jackson的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期格式来自API,如下所示:

I have a Date format coming from API like this:

  "start_time": "2015-10-1 3:00 PM GMT+1:00"

哪个是YYYY-DD-MM HH:MM am / pm GMT时间戳
我将该值映射到POJO中的Date变量。显然,它的显示转换错误。

Which is YYYY-DD-MM HH:MM am/pm GMT timestamp.I am mapping this value to a Date variable in POJO. Obviously, its showing conversion error.

我想知道两件事:


  1. 我需要用什么格式进行Jackson的转换?日期是一个很好的字段类型吗?

  2. 一般来说,有没有办法处理这些变量之前,他们被映射到Object成员由杰克逊?例如,改变格式,计算等。


推荐答案

日期是一个很好的字段为此键入。您可以通过使用 ObjectMapper.setDateFormat 来简化JSON解析:

Date is a fine field type for this. You can make the JSON parse-able pretty easily by using ObjectMapper.setDateFormat:

DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm a z");
myObjectMapper.setDateFormat(df);



是的。您有几个选项,包括实施自定义 JsonDeserializer ,例如延长 JsonDeserializer< Date> 这是一个很好的开始。

Yes. You have a few options, including implementing a custom JsonDeserializer, e.g. extending JsonDeserializer<Date>. This is a good start.

这篇关于日期格式映射到JSON Jackson的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

查看更多