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

查看:20
本文介绍了在 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天全站免登陆