备注:我是EclipseLink JAXB (MOXy)的领导者和JAXB (JSR-222)专家组的成员。
EclipseLink MOXy是符合JAXB(JSR-222)标准的实现。在EclipseLink 2.4.0中,我们引入了JSON绑定。由于MOXy是JAXB实现,您会发现MOXy生成的JSON输出将与基于相同元数据的XML输出非常一致。我将在下面举例说明。
域模型
下面是我将使用这个答案的域模型。有关在JAXB模型指定的命名空间信息的详细信息,请参阅:http://blog.bdoughan.com/2010/08/jaxb-namespaces.html
包信息
@XmlSchema(
namespace="http://www.example.com/A",
elementFormDefault=XmlNsForm.QUALIFIED,
xmlns={
@XmlNs(prefix="a",namespaceURI = "http://www.example.com/A"),
@XmlNs(prefix="b",namespaceURI = "http://www.example.com/B")
}
)
package forum13214306;
import javax.xml.bind.annotation.*;
客户
package forum13214306;
import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
String firstName;
@XmlElement(namespace="http://www.example.com/B")
String lastName;
}
XML处理
以下是域模型如何对应XML表示的示例。
演示
package forum13214306;
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Customer.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum13214306/input.xml");
Customer customer = (Customer) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(customer, System.out);
}
}
input.xml中/输出
<?xml version="1.0" encoding="UTF-8"?>
<a:customer xmlns:b="http://www.example.com/B" xmlns:a="http://www.example.com/A">
<a:firstName>Jane</a:firstName>
<b:lastName>Doe</b:lastName>
</a:customer>
JSON HANDLING - WITHOUT NAMESPACES
命名空间不是JSON概念,所以如果可以避免这种情况,我建议不要模拟它们。下面我将演示MOXy不需要它们。请注意,此处使用完全相同的域模型,JAXBContext
用于具有名称空间的XML文档。
jaxb。性能
要指定莫西为您的JSON提供你需要包括一个名为在同一个包jaxb.properties
与下面进入你的领域模型文件(见:http://blog.bdoughan.com/search/label/jaxb.properties)。
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
演示
要启用JSON绑定MEDIA_TYPE
属性需要在Marshaller
和Unmarshaller
被启用。
package forum13214306;
import java.io.File;
import javax.xml.bind.*;
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(Customer.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
File json = new File("src/forum13214306/input.json");
Customer customer = (Customer) unmarshaller.unmarshal(json);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
marshaller.marshal(customer, System.out);
}
}
input.json /输出
下面是输入和输出从运行演示代码。请注意JSON文档中没有模拟的名称空间信息。
{
"customer" : {
"firstName" : "Jane",
"lastName" : "Doe"
}
}
JSON处理 - 用模拟NAMESPACES
演示
如果你真的想模仿你的JSON文件中的命名空间,你可以利用上的NAMESPACE_PREFIX_MAPPER
财产Marshaller
和Unmarshaller
这样做。
package forum13214306;
import java.io.File;
import java.util.*;
import javax.xml.bind.*;
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(Customer.class);
Map<String, String> namespaceToPrefixMap = new HashMap<String, String>(2);
namespaceToPrefixMap.put("http://www.example.com/A", "a");
namespaceToPrefixMap.put("http://www.example.com/B", "b");
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
unmarshaller.setProperty(UnmarshallerProperties.JSON_NAMESPACE_PREFIX_MAPPER, namespaceToPrefixMap);
File json = new File("src/forum13214306/input.json");
Customer customer = (Customer) unmarshaller.unmarshal(json);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
marshaller.setProperty(MarshallerProperties.NAMESPACE_PREFIX_MAPPER, namespaceToPrefixMap);
marshaller.marshal(customer, System.out);
}
}
input.json /输出
{
"a.customer" : {
"a.firstName" : "Jane",
"b.lastName" : "Doe"
}
}
FOR更多信息
您需要包含您尝试使用的POJO类。也可能是你期望的JSON。 – StaxMan