解组JSON时出现ClassCastException

时间:2014-03-25 10:01:44

标签: java json unmarshalling

我有以下xml实体:

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

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

我试图以下列方式解组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);

但是我需要解组的JSON没有根节点,这就是它应该的样子。这是有问题的JSON:

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

使用此代码,我收到以下错误:

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

出了什么问题?

1 个答案:

答案 0 :(得分:0)

我认为您需要为每个字段添加@XmlElement

另外,不要忘记将@XmlRootElement添加到XdsJsonSubmissionSet@XmlElement添加到其字段中。