我正在尝试使用Jackson 2.0-RC3读取遗留的JSON代码,但是我坚持使用“嵌入式”对象.
给出以下JSON:
{
"title": "Hello world!",
"date": "2012-02-02 12:23:34".
"author": "username",
"author_avatar": "http://.../",
"author_group": 123,
"author_prop": "value"
}
如何将其映射到以下结构中:
class Author {
@JsonPropery("author")
private String name;
@JsonPropery("author_avatar")
private URL avatar;
@JsonProperty("author_group")
private Integer group;
...
}
class Item {
private String title;
@JsonProperty("date")
private Date createdAt;
// How to map this?
private Author author;
}
我试图用@JsonDeserialize做到这一点,但似乎我必须以这种方式映射整个Item对象.
解决方法:
要处理“嵌入式”对象,你应该使用@JsonUnwrapped – 它相当于Hibernate的@Ebueddable / @Embedded.
class Item {
private String title;
@JsonProperty("date")
private Date createdAt;
// How to map this?
@JsonUnwrapped
private Author author;
}