我有一个没有rootElement名称的JSON数据,如下所示:
{
name: "Anthony",
source: "DS"
}
我确实有一个java类,如下所示:
@XmlRootElement
@XmlAccessorOrder(XmlAccessOrder.UNDEFINED)
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
public class ObjectTest {
private String name;
private String source;
/* with Getter & Setter for "name" & "source" */
}
解组代码:
JAXBContext jc = JettisonMappedContext.newInstance(ObjectTest.class);
MappedNamespaceConvention c = new MappedNamespaceConvention();
JettisonMappedMarshaller u= new JettisonMappedMarshaller(jc, c);
String text = "{name: \"Anthony\", source: \"DS\"}";
u.unmarshal(new StringReader(text));
例外:
[javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"name"). Expected elements are <{}objectTest>]
如何根据上述内容进行解组?感谢
resteasy版本:1.1 RC2
答案 0 :(得分:1)
json设置不正确。
{"dataobjectname" : {name: "Anthony", source: "DS"}}
dataobjectname
应该与方法中定义的变量名匹配。