簡體   English   中英

Jaxb(json)解組錯誤,如何在沒有根元素名稱的情況下解組數據

[英]Jaxb (json) unmarshall error, how to unmarshall the data without the name of root element

我有一個不具有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

json設置不正確。

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

dataobjectname應該與方法中定義的變量名匹配。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM