从json解组嵌套对象 [英] unmarshalling nested objects from json

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

问题描述

我有传入的JSON字符串,我需要解组到JAXB带注释的对象。我正在使用jettison这样做。 JSON字符串如下所示:

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" 
    }
  }
}

ObjectA代码如下所示:

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

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

用于解组的代码:

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);

它不会抛出任何异常或警告。会发生什么是ObjectB被实例化,但它的属性都没有设置它们的值,即propertyA为null,propertyB的默认值为false。我一直在努力弄清楚为什么这不起作用。有人可以帮忙吗?

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?

推荐答案

注意:我是 EclipseLink JAXB(MOXy) 领导和 JAXB(JSR-222) 专家组。

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

模型上的JAXB映射似乎是正确的。下面是示例代码,我使用您在问题中给出的确切模型,通过EclipseLink MOXy提供JSON绑定:

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:

演示

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

以下是我使用的JSON。应该引用密钥 objectA objectB ,你的问题中没有这个。

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

更多信息

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

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

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