我有传入的 JSON 字符串,我需要解组为 JAXB 注释对象。我正在使用 jettison 来执行此操作。JSON 字符串如下所示:
{
objectA :
{
"propertyOne" : "some val",
"propertyTwo" : "some other val",
objectB :
{
"propertyA" : "some val",
"propertyB" : "true"
}
}
}
ObjectA 代码如下所示:
@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 类代码如下所示:
@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 为空,而 propertyB 的默认值为 false。我一直在努力弄清楚为什么这不起作用。有人可以帮忙吗?