[英]How to serialize a list with Jackson without the list name?
I'm using this code:我正在使用此代码:
XmlMapper objectMapper = new XmlMapper();
objectMapper.configure( ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true );
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
String xml = objectMapper.writeValueAsString(report);
To serialize this class:要序列化此类:
class Report {
List<Item> items;
}
The list of items is in an element called "items":项目列表位于名为“项目”的元素中:
<Report>
<items>
<item>
</item>
<item>
...
I would like it to serialize without the "items" element:我希望它在没有“items”元素的情况下进行序列化:
<Report>
<item>
</item>
<item>
...
Any suggestions how I can do this with jackson?有什么建议我可以用杰克逊做到这一点吗?
This is just a slight variation on the @Andreas answer.这只是@Andreas 答案的一个细微变化。
@JacksonXmlElementWrapper(localName = "ignoredName", useWrapping = false)
@JacksonXmlProperty(localName = "item")
private List<Item> itemList;
Use the @JacksonXmlElementWrapper
annotation to identify that it is a list of stuff and you don't want a wrapper element.使用
@JacksonXmlElementWrapper
注释来标识它是一个东西列表并且您不需要包装器元素。 Use the @JacksonXmlProperty
annotation to identify the element name.使用
@JacksonXmlProperty
注释来标识元素名称。
This will cause a repetition of <item>
xml elements in your output;这将导致在您的输出中重复
<item>
xml 元素; one per entry in the itemList
variable. itemList
变量中的每个条目一个。
Add the @JacksonXmlElementWrapper
annotation, specifying that you don't want a wrapper:添加
@JacksonXmlElementWrapper
注释,指定您不需要包装器:
@JacksonXmlElementWrapper(useWrapping = false)
List<Item> items;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.