简体   繁体   中英

ClassCastException when unmarshalling JSON

I have the following xml entity:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class DocumentDossier {

    private XdsJsonSubmissionSet submissionSet;
    private XdsJsonDocument documentEntry;
    private List<XdsJsonFolder> folder;
    private List<XdsJsonAssociation> association;
    ...

And I'm trying to unmarshall a JSON file in the following way:

    String content = IOUtils.toString(is);
    JSONObject obj = new JSONObject(content);
    Configuration config = new Configuration();
    MappedNamespaceConvention con = new MappedNamespaceConvention(config);
    XMLStreamReader xmlStreamReader = new MappedXMLStreamReader(obj, con);

    JAXBContext jc = JAXBContext.newInstance(DocumentDossier.class);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    DocumentDossier result = (DocumentDossier) unmarshaller.unmarshal(xmlStreamReader);

But the JSON I need to unmarshall doesn't have a root node, and that's the way it's supposed to be. Here's the JSON in question:

{
    "submissionSet":    {...},
    "documentEntry":    {...},
    "association":    {...}
}

Using this code, I get the following error:

java.lang.ClassCastException: XdsJsonSubmissionSet cannot be cast to DocumentDossier

What's wrong?

I think you need to add @XmlElement to each field.

Also don't forget to add @XmlRootElement to XdsJsonSubmissionSet , and @XmlElement to its fields.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM