2013-07-25 234 views
4

我想解开输入JSON到Eclipselink的JAXB对象。但是,当我尝试这样做时,我发现嵌套对象最终被设置为null。我可以尝试解开嵌套对象本身,它会一直运行,直到它必须解组一个嵌套的对象,然后将其设置为null。从JSON解组嵌套对象与JAXB

例如,借此类:

@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; 
     } 

    } 
} 

如果对象A有一个名为ObjectBs对象包含对象B的列表。当我尝试解组这个类时,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 
           } 
         }}}] 
     } 

回答

5

你没有提供你试图解编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 /输出

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

感谢您的快速回复。我试过使用JAXBContext.newInstance()的方法,但现在我得到:javax.xml.bind.PropertyException:name:eclipselink.media-value:application/json。另外,我添加了一些示例JSON。 –

+0

@PrashanDharmasena - 在类上创建'JAXBContext'时,'jaxb.properties'文件需要与创建'JAXBContext'类的一个包相同。问题中的类与您问题中的对象不匹配,您是否可以修复这些问题?你也可以看看JSON输出是什么,并将其与你试图输入的内容进行比较。 –

+1

感谢您的帮助,事实证明,我的输入JSON不匹配到类。 –