備註:我是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