如何解包多个 Map<String,String> 到 Json

How to unwrap more than one Map<String,String> to Json

如何将多个 Map 解包到 JSON。例子

public class Class {
Map<String,String> firstMap;
Map<String,String> secondMap;
}

我只能对一个地图字段使用@JsonAnyGetter。我也知道我可以使用自定义序列化器,但我在 class 中有更多字段,我不想更改反序列化方法。 首选 JSON 输出:

{
    "Name": "Name",
    "LastName": "LastName",
    "firstMapKey": "firstMapValue"
    "secondMapKey": "secondMapValue"
}

而不是:

{
    "Name": "Name",
    "LastName": "LastName",
    "firstMap": {
        "firstMapKey": "firstMapValue"
    },
    "secondMap": {
        "secondMapKey": "secondMapValue"
    }
}

@JonK 帮助了我:我添加了一个额外的地图并将两个地图合并到其中。对于附加地图,我对两个用于合并的地图使用了@JsonAnyGetter 和@JsonIgnore。

public class Class {
    @JsonIgnore
    Map<String,String> firstMap;
    @JsonIgnore
    Map<String,String> secondMap;

    Map<String,String> compositeMap

    @JsonAnyGetter
    public Map<String, String> getCompositeMap() {
        return compositeMap;
    }

    @JsonAnySetter
    public void setCompositeMap(Map<String, String> compositeMap) {
        this.compositeMap = compositeMap;
    }

}