【发布时间】:2020-05-07 10:28:21
【问题描述】:
所以我有 2 个课程:
public class Catalog{
private List<Country> countries = new ArrayList<>();
public List<Country> getCountries() {
return countries;
}
public void setCountries(List<Country> countries) {
this.countries = countries;
}
}
第二个:
public class Country{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
我想要什么? 创建 xml 但名称不同
<Catalogue>
<Countries>
<Country>
<name>RO</name>
</Country>
<Country>
<name>RO</name>
</Country>
</Countries>
</Catalogue>
我使用以下方式编写 xml:
Catalogue catalogue = new Catalogue();
Country country = new Country();
country.setName("RO");
catalogue.getCountries().add(country);
catalogue.getCountries().add(country);
String xml = xmlMapper.writeValueAsString(catalogue); // serializing
System.out.println("The xml is " + xml);
我该怎么做? 我尝试在元素、@JsonGetter 和 @JsonSetter 上使用 @JsonProperty,但我无法做到这一点。
当我添加这些注释时,它会执行类似的操作
<Countries>
<Countries>
<name>RO</name>
</Countries>
</Countries>
【问题讨论】:
标签: json list arraylist jackson