[英]How to serialize a list with Jackson without the list name?
我正在使用此代碼:
XmlMapper objectMapper = new XmlMapper();
objectMapper.configure( ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true );
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
String xml = objectMapper.writeValueAsString(report);
要序列化此類:
class Report {
List<Item> items;
}
項目列表位於名為“項目”的元素中:
<Report>
<items>
<item>
</item>
<item>
...
我希望它在沒有“items”元素的情況下進行序列化:
<Report>
<item>
</item>
<item>
...
有什么建議我可以用傑克遜做到這一點嗎?
這只是@Andreas 答案的一個細微變化。
@JacksonXmlElementWrapper(localName = "ignoredName", useWrapping = false)
@JacksonXmlProperty(localName = "item")
private List<Item> itemList;
使用@JacksonXmlElementWrapper
注釋來標識它是一個東西列表並且您不需要包裝器元素。 使用@JacksonXmlProperty
注釋來標識元素名稱。
這將導致在您的輸出中重復<item>
xml 元素; itemList
變量中的每個條目一個。
添加@JacksonXmlElementWrapper
注釋,指定您不需要包裝器:
@JacksonXmlElementWrapper(useWrapping = false)
List<Item> items;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.