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

发布于 2024-10-21 19:00:48 字数 944 浏览 4 评论 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>]

如何根据上面的内容进行解组?感谢

resteasy版本:1.1 RC2

I have a JSON data that do not have a rootElement's name which is as follow:

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

I do have a java class which is as follow for the unmarshalling:

@XmlRootElement
@XmlAccessorOrder(XmlAccessOrder.UNDEFINED)
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
public class ObjectTest {
  private String name;
  private String source;

  /* with Getter & Setter for "name" & "source" */
}

Unmarshalling code:

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));

Exception:

[javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"name"). Expected elements are <{}objectTest>]

How can I do the unmarshalling based on the content above? Thanks

resteasy version: 1.1 RC2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魄砕の薆 2024-10-28 19:00:48

json 设置不正确。

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

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

The json is not set properly.

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

dataobjectname should match with the variable name defined in the method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文