【发布时间】:2013-02-27 17:42:54
【问题描述】:
使用 Marshaller 将 java 对象转换为 XML 非常容易。但我需要单独使用 marshaller 将 java 对象转换为 JSON。我知道使用 gson 或 Xstream 之类的东西很好。但是我需要使用 Marshaller。如何实现它?
提前致谢。
【问题讨论】:
标签: java json jakarta-ee jaxb marshalling
使用 Marshaller 将 java 对象转换为 XML 非常容易。但我需要单独使用 marshaller 将 java 对象转换为 JSON。我知道使用 gson 或 Xstream 之类的东西很好。但是我需要使用 Marshaller。如何实现它?
提前致谢。
【问题讨论】:
标签: java json jakarta-ee jaxb marshalling
注意:我是EclipseLink JAXB (MOXy) 的负责人,也是JAXB (JSR-222) 专家组的成员,
如果您使用 MOXy 作为您的 JAXB 提供程序,以下是如何做到这一点。
JAVA 模型
客户
import java.util.*;
import javax.xml.bind.annotation.*;
@XmlRootElement(namespace="http://www.example.com")
@XmlType(namespace="http://www.example.com")
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
@XmlAttribute
private int id;
@XmlElement(namespace="http://www.example.com")
private String firstName;
@XmlElement(namespace="http://www.example.com", nillable=true)
private String lastName;
@XmlElement(namespace="http://www.example.com")
private List<PhoneNumber> phoneNumbers = new ArrayList<PhoneNumber>();
}
电话号码
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class PhoneNumber {
@XmlAttribute
private String type;
@XmlValue
private String number;
}
jaxb.properties
要将 MOXy 指定为您的 JAXB 提供程序,您需要在与您的域模型相同的包中包含一个名为 jaxb.properties 的文件,其中包含以下条目(请参阅:http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html)
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
演示代码
input.xml
<?xml version="1.0" encoding="UTF-8"?>
<ns0:customer xmlns:ns0="http://www.example.com" id="123">
<ns0:firstName>Jane</ns0:firstName>
<ns0:lastName xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<ns0:phoneNumbers type="work">555-1111</ns0:phoneNumbers>
</ns0:customer>
演示
在下面的演示代码中,我们将使用相同的 JAXB 元数据将 XML 文档转换为 Java 对象,然后将这些对象转换回 JSON。使用 MOXy,您可以通过在 Marshaller 上设置属性来指定 JSON 输出。
import java.io.File;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.MarshallerProperties;
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/forum15357366/input.xml");
Customer customer = (Customer) unmarshaller.unmarshal(xml)
;
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
marshaller.marshal(customer, System.out);
}
}
JSON 输出
以下是 JSON 输出。请注意没有与名称空间或 XML 属性相对应的指示符。另请注意,大小为 1 的集合被正确表示为 JSON 数组(其他一些方法存在问题)。
{
"id" : 123,
"firstName" : "Jane",
"lastName" : null,
"phoneNumbers" : [ {
"type" : "work",
"value" : "555-1111"
} ]
}
【讨论】:
pom.xml,您可以将其用作指南:github.com/bdoughan/blog20110819/blob/master/pom.xml
JsonMarshaller 是一个 Java 1.5 库,它允许将 JSON 对象编组到 Java 对象和从 Java 对象解组。该项目的目标首先是易用性、透明性和静态类型安全。 示例
如果你有以下 Java 类:
JAVA:
@Entity
class Book {
@Value
private String title;
@Value
private String isbn;
@Value
private Set<author> authors;
}
@Entity
class Author {
@Value
private String firstName;
@Value
private String lastName;
}
你创建了一个新的 Book() 并用它可以编组的信息填充它:
JSON:
{title: "java",
isbn: "122333",
authors: [{firstName: "Herbert", lastName: "Shield"},
{firstName: "Pascal", lastName: "Perez"}]}
【讨论】: