日期格式映射到JSON Jackson [英] Date format Mapping to 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.
我想知道两件事:
- 我需要用什么格式进行Jackson的转换?日期是一个很好的字段类型吗?
- 一般来说,有没有办法处理这些变量之前,他们被映射到Object成员由杰克逊?例如,改变格式,计算等。
推荐答案
我需要用什么格式进行Jackson的转换?日期是一个很好的字段类型吗?
What is the formatting I need to use to carry out conversion with Jackson? Is Date a good field type for this?
日期
是一个很好的字段为此键入。您可以通过使用 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);
一般来说,是否有一种处理变量的方法,映射到杰克逊的对象成员?例如,更改格式,计算等。
In general, is there a way to process the variables before they get mapped to Object members by Jackson? Something like, changing the format, calculations, etc.
是的。您有几个选项,包括实施自定义 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!