本文介绍了Jackson用时间戳字段反序列化JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的字符串:
{
"debug":"false",
"switchTime":"2017-04-12 17:04:42.896026"
}
我正在尝试通过这种方法获取对象:
I'm trying to get object in such approach:
new ObjectMapper().readValue(string, MyObject.class);
和MyObject类:
And MyObject class:
class MyObject {
private Boolean debug;
private Timestamp switchTime;
//...getters, setters, constructors
}
我有这样的例外:
com.fasterxml.jackson.databind.exc.InvalidFormatException:
Can not deserialize value of type java.sql.Timestamp from String
"2017-04-12 17:04:42.896026": not a valid representation (error:
Failed to parse Date value '2017-04-12 17:04:42.896026':
Can not parse date "2017-04-12 17:04:42.896026Z": while it seems
to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS'Z'',
parsing fails (leniency? null)) at [Source:
{"debug":"false", "switchTime":"2017-04-12 17:04:42.896026"};
我不明白为什么...如果我在调试模式下使用Timestamp.valueOf()和"2017-04-12 17:04:42.896026"-我成功了
I don't understand why...If i use in debug mode Timestamp.valueOf() with "2017-04-12 17:04:42.896026" - i have success
推荐答案
我认为您需要使用@JsonFormat
注释设置预期的日期/时间格式,如下所示.
I think you need to set the expected date/time format using @JsonFormat
annotation as shown below.
class MyObject {
private Boolean debug;
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss.SSS")
private Timestamp switchTime;
//...getters, setters, constructors
}
您还可以将时区设置为@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss.SSS",timezone="PST")
You can also set timezone as @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss.SSS",timezone="PST")
这篇关于Jackson用时间戳字段反序列化JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!