我们如何使用 JAXB 编码/解码包含列表的 JSON 中的根元素?
所以它会将 JSON 作为
{
"tag" : [
{
"id" : "a",
"id2": "aa"
},
{
"id" : "b",
"id2" : "bb"
},
{
"id" : "c",
"id2" : "cc"
}
]
}
我正在使用通过 Jettison 支持 JSON 的 Apache CXF。
Java 类可能如下所示。可以对列表使用 XmlList 注释,并使用 XmlValue 将该列表放在根元素中。问题是 XmlValue 不会采用用户定义的类型。
@XmlRootElement(name = "tag")
public class test
{
@XmlList
@XmlValue
private List<UserDefinedType> testList;
}
有没有办法解决这个问题。我需要它来解码传入的 JSON。从这里得到这个想法 http://bdoughan.blogspot.com/2010/09/jaxb-collection-properties.html
最佳答案
这应该适用于您提到的 JSON 格式。但是,如果您也想编码/解码为某种 XML 格式,它可能不起作用。
@XmlRootElement
public class Test {
@XmlElement(name = "tag")
private List<UserDefinedType> testList;
}
public class UserDefinedType {
@XmlElement(name = "id")
private String someId;
@XmlElement(name = "id2")
private String someId2;
}
关于java - 使用 jaxb 将列表表示为根元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4729385/