问题描述
我们如何使用JAXB编组/解组包含列表的JSON中的根元素?
How can we marshal/unmarshal the root element in a JSON that contains a list using JAXB?
因此它将JSON设为
{
"tag" : [
{
"id" : "a",
"id2": "aa"
},
{
"id" : "b",
"id2" : "bb"
},
{
"id" : "c",
"id2" : "cc"
}
]
}
我正在使用通过Jettison支持JSON的Apache CXF。
I am using Apache CXF which supports JSON through Jettison.
Java类可能看起来像一个在下面。可以对列表使用XmlList注释,使用XmlValue在根元素中使用该列表。问题是XmlValue不会采用用户定义的类型。
The Java class could look like the one below. One could use a XmlList annotation for the list, and XmlValue for having that list in the root element. The problem is XmlValue would not take a user-defined type.
@XmlRootElement(name = "tag")
public class test
{
@XmlList
@XmlValue
private List<UserDefinedType> testList;
}
有没有办法解决这个问题。我需要这个来解组传入的JSON。从这里得到这个想法
Is there a way to get around this. I need this to work for un-marshalling an incoming JSON. Got this idea from herehttp://bdoughan.blogspot.com/2010/09/jaxb-collection-properties.html
推荐答案
这应该适用于JSON格式你提及。但是,如果您想要对某种XML格式进行编组/解组,也可能无法正常工作。
This should work for the JSON format you mentioned. However, it might not work if you want to marshall/unmarshall to a certain XML format too.
@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;
}
这篇关于使用jaxb将列表表示为根元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!