使用JAXB从JSON中解组嵌套对象

5
我正在尝试使用Eclipselink将输入的JSON转换为JAXB对象。但是,当我尝试这样做时,我发现嵌套的对象最终被设置为null。我可以尝试单独对嵌套对象进行解组合,它可以正常工作,直到它必须解组另一个嵌套对象,也被设置为null。
例如,考虑这个类:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "event", propOrder = {
"objectBs"
})
public class ObjectA
implements Serializable
{

    private final static long serialVersionUID = 56347348765454329L;
    @XmlElement(required = true)
    protected ObjectA.ObjectBs objectBs;

    public ObjectA.ObjectBs getObjectBs() {
        return objectBs;
    }

    public void setObjectBs(ObjectA.ObjectBs value) {
        this.objectBs = value;
    }

    public boolean isSetObjectBs() {
        return (this.objectBs!= null);
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "objectB"
    })
    public static class ObjectBs
        implements Serializable
    {

        private final static long serialVersionUID = 56347348765454329L;
        @XmlElement(required = true)
        protected List<ObjectB> objectB;

        public List<ObjectB> getObjectB() {
            if (objectB == null) {
                objectB = new ArrayList<ObjectB>();
            }
            return this.objectB;
        }

        public boolean isSetObjectB() {
            return ((this.objectB!= null)&&(!this.objectB.isEmpty()));
        }

        public void unsetObjectB() {
            this.objectB = null;
        }

    }
}

ObjectA对象包含一个名为ObjectBs的对象,其中包含一组ObjectB。 当我尝试将此类取消编组时,任何其他字段都会正确填充ObjectA,并创建ObjectBs对象,但是ObjectB列表将为空。 但是,如果我单独取消编组一个ObjectB,它将被创建并填充其字段,直到其自己的嵌套对象。

这是我用来取消编组JSON的代码:

JAXBContext jc = JAXBContextFactory.createContext(
    "com.package1"
    + ":com.package2"
    + ":com.package3"
    , null);
Unmarshaller um = jc.createUnmarshaller();
um.setProperty("eclipselink.media-type", "application/json"); 
um.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
ObjectA objA = unmarshaller.unmarshal(new StreamSource(
    new StringReader(json)), ObjectA.class).getValue();

以下是一些JSON示例:

        {
                "objectBs": [
                    {
                        "a": "blahblah",
                        "b": 123456,
                        "c": "abc blah 123 blah",
                        "nestedObject": {
                            "evenMoreNestedObjects": {
                                "d": "blah",
                                "e": "blahblahblah",
                                "anotherNestedObject": {
                                    "x": 25,
                                    "y": 50
                                }
                        }}}]
        }

类似:https://stackoverflow.com/questions/16365788/unmarshalling-nested-objects-from-json - Vadzim
1个回答

6

您没有提供要解封的JSON,但我进行了一些反向工程,下面是一个使用您在问题中发布的模型有效的示例:

import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;
import org.eclipse.persistence.jaxb.MarshallerProperties;
import org.eclipse.persistence.jaxb.UnmarshallerProperties;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(ObjectA.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
        unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
        StreamSource json = new StreamSource("src/forum17866155/input.json");
        ObjectA objectA = unmarshaller.unmarshal(json, ObjectA.class).getValue();

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
        marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(objectA, System.out);
    }

}

input.json/Output

{
   "objectBs" : {
      "objectB" : [ {
      }, {
      } ]
   }
}

感谢您的快速回复。我尝试了您使用JAXBContext.newInstance()的方法,但现在我收到了javax.xml.bind.PropertyException: name: eclipselink.media-type value: application/json的错误。此外,我添加了一些JSON示例。 - Prashan Dharmasena
@PrashanDharmasena - 在创建JAXBContext时,jaxb.properties文件需要与JAXBContext所创建的类之一在同一个包中。您问题中的类与对象不匹配,您能否修复一下?此外,您可以查看JSON输出并将其与您尝试输入的内容进行比较。 - bdoughan
1
感谢您的帮助,事实证明我的输入JSON与类不匹配。 - Prashan Dharmasena

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接