gpt4 book ai didi

java - 如何使用 Jackson 将其序列化为 xml?

转载 作者:行者123 更新时间:2023-12-02 10:38:00 28 4
gpt4 key购买 nike

给定这个类

class Report {
public String total;
public Map monthly;

public Report () {
total = "10";
monthly = new HashMap();
monthly.put("MAR", 5);
monthly.put("JUN", 5);
}
}

我想生成此 XML:

<Report>
<total>10</total>
<MAR>5</MAR>
<JUN>5</JUN>
</Report>

但它实际上生成了这个 XML:

 <Report>
<total>10</total>
<monthly>
<MAR>5</MAR>
<JUN>5</JUN>
</monthly>
</Report>

如果我在 montly 声明之前添加 @JsonIgnoremontly 元素就会消失,但 total 也会消失>!?

<Report>
<MAR>5</MAR>
<JUN>5</JUN>
</Report>

最佳答案

向您的属性添加访问器方法,并使用 @com.fasterxml.jackson.annotation.JsonAnyGetter 注释 getMonthly

public class Report {

private String total;

private Map monthly;

public Report () {
total = "10";
monthly = new HashMap<>();
monthly.put("MAR", 5);
monthly.put("JUN", 5);
}

public String getTotal() {
return total;
}

@JsonAnyGetter
public Map getMonthly() {
return monthly;
}

}

关于java - 如何使用 Jackson 将其序列化为 xml?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53136623/

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