gpt4 book ai didi

java - 使用 Jackson 自定义 POJO 序列化

转载 作者:行者123 更新时间:2023-12-02 03:28:32 24 4
gpt4 key购买 nike

我有一个像下面这样的pojo,当我序列化这个Pojo时我想要 map 的元素将显示为 json 的根元素,而不是 testMap

下的嵌套元素
public class ProxyConfig implements Serializable {

private String test;

private Integer intValue;

Map<String, String> testMap;

示例假设 testMap 中有两个元素,那么序列化的 ProxyConfig 应如下所示:

{
"test" : "testValue",
"intValue" : 20,
"testMapkey1" : "value1",
"testMapkey1" : "value1",
}

不确定如何实现 CustomSerializer 来实现此目的,任何输入都会有帮助

最佳答案

在实例中序列化 map 时,您可以使用 @JsonAnyGetterMap 中的键值作为普通属性获取:

@JsonAnyGetter
public Map<String, String> getTestMap() {
return testMap;
}

关于java - 使用 Jackson 自定义 POJO 序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38422267/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com