Jaxb(json)unmarshal错误,如何在没有根元素名称的情况下解组数据

时间:2011-03-14 05:02:05

标签: java json jaxb resteasy jettison

我有一个没有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

1 个答案:

答案 0 :(得分:1)

json设置不正确。

{"dataobjectname" : {name: "Anthony", source: "DS"}}

dataobjectname应该与方法中定义的变量名匹配。