0

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

如何根据上述内容进行解组?谢谢

轻松版:1.1 RC2

4

1 回答 1

1

json 设置不正确。

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

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

于 2012-01-31T06:13:01.123 回答