简体   繁体   English

解封JSON时发生ClassCastException

[英]ClassCastException when unmarshalling JSON

I have the following xml entity: 我有以下xml实体:

@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: 我正在尝试通过以下方式解组JSON文件:

    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. 但是我需要解组的JSON没有根节点,这就是应该的样子。 Here's the JSON in question: 这是有问题的JSON:

{
    "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. 我认为您需要在每个字段中添加@XmlElement

Also don't forget to add @XmlRootElement to XdsJsonSubmissionSet , and @XmlElement to its fields. 同样不要忘记将@XmlRootElement添加到XdsJsonSubmissionSet ,并将@XmlElement到其字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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