gpt4 book ai didi

json - 使用 JAXB 从 JSON 解码嵌套对象

转载 作者:行者123 更新时间:2023-12-04 18:44:11 24 4
gpt4 key购买 nike

我正在尝试使用 Eclipselink 将输入 JSON 解码到 JAXB 对象中。但是,当我尝试这样做时,我发现嵌套对象最终被设置为 null。我可以尝试自己解码嵌套对象,它会一直工作,直到它必须解码另一个嵌套对象,然后将其设置为 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;
}

}
}

其中 ObjectA 有一个名为 ObjectBs 的对象,其中包含一个 ObjectB 列表。当我尝试解码此类时,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
}
}}}]
}

最佳答案

您没有提供您尝试解码的 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" : [ {
}, {
} ]
}
}

关于json - 使用 JAXB 从 JSON 解码嵌套对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17866155/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com