Jackson将嵌套列表反序列化为键值对象

时间:2018-09-03 05:09:33

标签: jackson2 jackson-databind

我需要使用杰克逊反序列化/序列化json / object。我需要反序列化“ UA”:{“ featureId”:2,“ enabled”:true}作为列表,其中“ UA”是FeatureAccess类的属性(名称),应将其映射为对象的键。有没有办法使用杰克逊实现这一目标?我还需要使用相同的对象结构将其序列化回json。我试图编写自定义反序列化器,但是没有被调用。

我的班级结构如下。

public class FeatureUI implements Serializable {
    @JsonUnwrapped
    private Feature feature;
    private int count;
    // @JsonDeserialize(using = CustomDeserializer.class)
    private List<FeatureAccess> featuresAccess;
}

public class Feature implements Serializable {
    private int featureId;
    private String name;
    private Integer parentId;
}

public class FeatureAccess implements Serializable {
    private int featureId;
    private String name;
    private Boolean enabled;
}

输出/输入格式

[
    {
        "featureId": 1,
        "parentId": null,
        "PU": {
                "featureId": 1
                "enabled": true
            }
    },
    {
        "featureId": 2,
        "parentId": null,
        "UA": {
                "featureId": 2,
                "enabled": true
            }
    },
    {
        "featureId": 8,
        "parentId": null,
        "OA": {
                "featureId": 8,
                "enabled": false
            },
        "PU": {
                "featureId": 8,
                "enabled": true
            },
        "UA": {
                "featureId": 8,
                "selected": true
            }
    }
]

0 个答案:

没有答案