unmarshalling nested objects from json

一笑奈何 提交于 2019-12-02 05:33:44

问题


I have incoming JSON strings and I need to unmarshall into JAXB annotated objects. I am using jettison to do this. JSON string looks like this:

{ 
  objectA : 
  { 
    "propertyOne" : "some val", 
    "propertyTwo" : "some other val",
    objectB : 
    {
      "propertyA" : "some val",
      "propertyB" : "true" 
    }
  }
}

The ObjectA code looks like this:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "objectA")
public class ObjectA {
    @XmlElement(required = true)
    protected String propertyOne;
    @XmlElement(required = true)
    protected String propertyTwo;
    @XmlElement(required = true)
    protected ObjectB objectB;
}

The ObjectB class code looks like this:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "objectB")
public class ObjectB {
    @XmlElement(required = true)
    protected String propertyA;
    @XmlElement(required = true)
    protected boolean propertyB;
}

The code used to unmarshall:

JAXBContext jc = JAXBContext.newInstance(OnjectA.class);
JSONObject obj = new JSONObject(theJsonString);
Configuration config = new Configuration();

MappedNamespaceConvention con = new MappedNamespaceConvention(config);
XMLStreamReader xmlStreamReader = new MappedXMLStreamReader(obj,con);
Unmarshaller unmarshaller = jc.createUnmarshaller();

ObjectA obj = (ObjectA) unmarshaller.unmarshal(xmlStreamReader);

It doesn't throw any exceptions or warnings. What happens is that ObjectB is instantiated but none of its properties get their values set, i.e. propertyA is null and propertyB gets its default value of false. I've been struggling to figure out why this doesn't work. Can someone help?


回答1:


Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

The JAXB mappings on your model appear to be correct. Below is sample code where I used your exact model as given in your questions with the JSON-binding available through EclipseLink MOXy:

Demo

package forum16365788;

import java.io.File;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextProperties;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String, Object>(1);
        properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
        JAXBContext jc = JAXBContext.newInstance(new Class[] {ObjectA.class}, properties);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File json = new File("src/forum16365788/input.json");
        ObjectA objectA = (ObjectA) unmarshaller.unmarshal(json);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(objectA, System.out);
    }

}

input.json/Output

Below is the JSON I used. The keys objectA and objectB should be quoted, you don't have this in your question.

{
   "objectA" : {
      "propertyOne" : "some val",
      "propertyTwo" : "some other val",
      "objectB" : {
         "propertyA" : "some val",
         "propertyB" : true
      }
   }
}

For More Information

  • http://blog.bdoughan.com/2011/08/json-binding-with-eclipselink-moxy.html
  • http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html


来源:https://stackoverflow.com/questions/16365788/unmarshalling-nested-objects-from-json

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!