使用JAXB从JSON解组嵌套对象 [英] Unmarshalling nested objects from JSON with JAXB

查看:277
本文介绍了使用JAXB从JSON解组嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Eclipselink将输入JSON解组为JAXB对象。但是,当我尝试这样做时,我发现嵌套对象最终被设置为null。我可以尝试自己解组嵌套对象,它将一直工作,直到它必须解组另一个嵌套对象,然后再设置为null。

I'm trying to unmarshall input JSON into JAXB objects with Eclipselink. However, when I try to do this, I find that nested objects end up being set as null. I can try and unmarshall the nested object by itself, and it will work up until the point that it has to unmarshall a further nested object, which is also then set to 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;
        }

    }
}

Where ObjectA有一个名为ObjectBs的对象,它包含一个ObjectB列表。当我尝试解组此类时,ObjectA具有的任何其他字段将被正确填充,并且将创建ObjectBs对象,但ObjectB的列表将为空。但是,如果我自己解组一个ObjectB,它将被填充,直到它自己的嵌套对象为止。

Where ObjectA has an object called ObjectBs which contains a list of ObjectB. When I try unmarshalling this class, any other field that ObjectA has would be correctly filled, and the ObjectBs object would be created, but the list of ObjectB would be empty. But, if I unmarshall just one ObjectB by itself, it will be created with it's fields filled, up until it's own nested objects.

这是我正在使用的代码解组JSON:

This is the code I'm using to unmarshall the 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:

And some example JSON:

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


推荐答案

您没有提供要解组的JSON,但我做了一些逆向工程,下面是一个使用你发布的模型的例子你的问题:

You didn't provide the JSON you are trying to unmarshal, but I've done a little reverse engineering and below is an example that works using the model you posted in your question:

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 /输出

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

这篇关于使用JAXB从JSON解组嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆