0

我正在使用 Java 1.8 和 FasterXML。

假设您有一个由 JSON 数组组成的数组列表:

[
    {
        "id": "8",
        "musician": {
            "band": {
                "bandId": "104"
            },
            "genre": {
                "type": "country",
                "typeId": 123
            },
            "musician": {
                "id": "87"
            }
        }
     },
     {
        "id": "9",
        "musician": {
            "band": {
                "bandId": "104"
            },
            "genre": {
                "type": "country",
                "typeId": 123
            },
            "musician": {
                "id": "88"
            }
        }
     }
]

public class Musician {

    private int id;
    private Band band;
    private Genre genre;

    // How to marshal this?
    private Musician musician;  

}

public class Band {

    private bandId;
}

public class Genre {

    private String type;
    private String typeId;
}

如果您需要从类中编组相同类型(即 ),如何设置顶级Musician类?

有注释可以使用吗?

应该,我只是将内部的 Musician 类命名为别的吗?

4

1 回答 1

0
public class Musician {

    private int id;
    private Band band;
    private Genre genre;

    @JsonIgnoreProperties(["band", "genre"])
    private Musician musician;  

}

(我还建议将“音乐家”属性重命名为可以阐明外部属性和内部属性之间的实际关系的东西。)

来源:Jackson json 双向对象引用

于 2020-06-12T04:47:06.630 回答