我有一些JSON,其时间戳以毫秒为单位:
{"foo":"bar","timestamp":1386280997}
要求杰克逊将其反序列化为具有时间戳的DateTime字段的对象,结果为1970-01-17T01:11:25.983Z
,这是纪元后不久的时间,因为杰克逊假设它在纳秒。除了撕掉JSON并添加一些零之外,我怎样才能让杰克逊理解毫秒时间戳呢?
编辑 - 显示此内容的一些测试代码(Groovy语法):
class TimestampThing {
DateTime timestamp
@JsonCreator
public TimestampThing(@JsonProperty('timestamp') DateTime timestamp) {
this.timestamp = timestamp
}
}
还有几个测试,第一个是毫秒,第二个是微秒:
class TimestampTest extends GroovyTestCase {
private ObjectMapper mapper = new ObjectMapper()
void testMillisecondTimestamp() {
String json = '{"timestamp":1386280997}'
TimestampThing thing = mapper.readValue(json, TimestampThing.class)
println thing.getTimestamp()
}
void testNanosecondTimestamp() {
String json = '{"timestamp":1386280997000}'
TimestampThing thing = mapper.readValue(json, TimestampThing.class)
println thing.getTimestamp()
}
}
运行测试类的出局是:
1970-01-16T20:04:40.997-05:00
2013-12-05T17:03:17.000-05:00