java - 用 jackson 序列化日期列表

标签 java json serialization jackson date-format

我正在尝试序列化一个包含日期列表的对象,我想序列化为特定格式 (yyyy-MM-dd) 的 JSON 日期列表 (String)。

private List<Date> executionDates;

会变成这样:

"executionDates": [
  "2016-07-22",
  "2016-07-23",
  "2016-07-24"
]

可以用注解来做吗?

提前致谢。

最佳答案

我找到了解决方案。我不得不在这样的注释中使用属性 contentUsing 而不是 using :

@JsonSerialize(contentUsing = JsonDateSerializer.class)

contentUsing 属性用于集合。来自类(class)文档:

Serializer class to use for serializing contents (elements of a Collection/array, values of Maps) of annotated property. Can only be used on properties (methods, fields, constructors), and not value classes themselves (as they are typically generic).

关于java - 用 jackson 序列化日期列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38528816/

相关文章:

c# - 如何防止 WebMethod 序列化 json 响应

java - 字符串分词器

java - Java HTTP 服务器错误 : Unexpected end of file from server

php - ASyncTask HTTP 错误

.net - 有什么方法可以使 DataContractJsonSerializer 正确序列化字典吗?

php - 在 php 中使用序列化将关联数组存储在数据库中

java - 自动填充有用类型的实例变量的 Eclipse 快捷方式?

java - 意外的 Java 性能

java - 使用观察者模式获取主类中的数据

ios - Swift 中的序列化、反序列化到 JSON