user658240
user658240

Reputation: 1

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

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

Upvotes: 0

Views: 3038

Answers (1)

saurabh
saurabh

Reputation: 271

The json is not set properly.

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

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

Upvotes: 1

Related Questions