【问题标题】:Jaxb (json) unmarshall error, how to unmarshall the data without the name of root elementJaxb(json)解组错误,如何解组没有根元素名称的数据
【发布时间】:2011-03-14 05:02:05
【问题描述】:

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

【问题讨论】:

    标签: java json jaxb resteasy jettison


    【解决方案1】:

    json 设置不正确。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-23
      • 2020-08-09
      • 2012-06-23
      相关资源
      最近更新 更多