阅读Jackson中的嵌入对象 [英] Read embedded object in Jackson

查看:131
本文介绍了阅读Jackson中的嵌入对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Jackson 2.0-RC3读取遗留的JSON代码,但是我坚持使用嵌入式对象。

I'm trying to read a legacy JSON code using Jackson 2.0-RC3, however I'm stuck with an "embedded" object.

给出以下JSON :

{
    "title": "Hello world!",
    "date": "2012-02-02 12:23:34".
    "author": "username",
    "author_avatar": "http://.../",
    "author_group": 123,
    "author_prop": "value"
}

如何将其映射到以下结构中:

How can I map it into the following structure:

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 对象。

I was trying to do that with @JsonDeserialize but it seems that I'd have to map the entire Item object that way.

推荐答案

要处理嵌入对象,你应该使用 @JsonUnwrapped - 它相当于Hibernate的 @Embeddable / @Embedded

To deal with an "embedded" object you should use @JsonUnwrapped — it's an equivalent of the Hibernate's @Embeddable/@Embedded.

class Item {
    private String title;

    @JsonProperty("date")
    private Date createdAt;

    // How to map this?
    @JsonUnwrapped
    private Author author;
}

这篇关于阅读Jackson中的嵌入对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆