原始答案
MOXy 对键进行分组tiger
并elephant
避免重复它们。
更新#1
所以不可能得到像 { 'animals': [ {'@type': 'tiger'}, {'@type':elephant'}, ... ] } 这样的 JSON?
是的,这是可能的,你只需要这样映射它:
动物园
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class Zoo {
private List<Animal> animals;
}
动物
import javax.xml.bind.annotation.*;
@XmlSeeAlso({Elephant.class, Tiger.class})
@XmlAccessorType(XmlAccessType.FIELD)
public abstract class Animal {
}
演示
import java.util.*;
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;
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>();
properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
properties.put(JAXBContextProperties.JSON_ATTRIBUTE_PREFIX, "@");
JAXBContext jc = JAXBContext.newInstance(new Class[] {Zoo.class}, properties);
Unmarshaller unmarshaller = jc.createUnmarshaller();
StreamSource json = new StreamSource("src/forum19384491/input.json");
Zoo zoo = unmarshaller.unmarshal(json, Zoo.class).getValue();
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(zoo, System.out);
}
}
输入.json/输出
{
"animals" : [ {
"@type" : "tiger"
}, {
"@type" : "elephant"
}, {
"@type" : "tiger"
} ]
}
更新#2
如果您想保留当前的 XML 表示并仅更改 JSON 表示,您可以使用 MOXy 的外部映射文档扩展(参见: http ://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html )
映射文件 (oxm.xml)
我们将使用 MOXy 的外部映射文档来更改类animals
上字段的映射Zoo
。
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="forum19384491">
<java-types>
<java-type name="Zoo">
<java-attributes>
<xml-element java-attribute="animals"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
演示
JAXBContext
在下面的演示代码中,我们在同一个域模型上创建了 2 个实例。JSON 的一个利用外部映射文档来自定义映射。 input.xml
是您问题中的 XML 文档。
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 {
JAXBContext xmlJC = JAXBContext.newInstance(Zoo.class);
Unmarshaller unmarshaller = xmlJC.createUnmarshaller();
File xml = new File("src/forum19384491/input.xml");
Zoo zoo = (Zoo) unmarshaller.unmarshal(xml);
Map<String, Object> properties = new HashMap<String, Object>(4);
properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum19384491/oxm.xml");
properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
properties.put(JAXBContextProperties.JSON_ATTRIBUTE_PREFIX, "@");
JAXBContext jsonJC = JAXBContext.newInstance(new Class[] {Zoo.class}, properties);
Marshaller marshaller = jsonJC.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(zoo, System.out);
}
}
输出
下面是运行演示代码的输出。
{
"animals" : [ {
"@type" : "tiger",
"name" : "Richard",
"furry" : true
}, {
"@type" : "elephant",
"name" : "Otis",
"furry" : false
}, {
"@type" : "tiger",
"name" : "Kirk",
"furry" : true
} ]
}