2013-03-12 20 views
14

使用Marshaller可以很容易地将java对象转换为XML。但我需要单独使用marshaller将java对象转换为JSON。我知道使用gson或Xstream就好,但我需要使用Marshaller.How才能实现它?使用Marshaller将Java对象转换为Json

在此先感谢。

回答

10

说明:我是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

要指定莫西为您的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 

演示代码

我nput.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属性相对应的指示符。还要注意,第一个大小的集合被正确地表示为JSON数组(一些其他方法的问题)。

{ 
    "id" : 123, 
    "firstName" : "Jane", 
    "lastName" : null, 
    "phoneNumbers" : [ { 
     "type" : "work", 
     "value" : "555-1111" 
    } ] 
} 
+0

@BDoughan:是的,我也在您的博客中看到过这一点。但是我在JAXBContextFactory上得到了ClassNotFound异常。我已经包含了jaxb.properties,并且我也在pom.xml中放置了一个依赖项。请亲切指导我对该类的正确依赖关系 – Arun 2013-03-12 11:15:15

+0

@ user1933756 - 以下是我在GitHub上托管的示例中的一个'pom.xml',您可以将其用作指导:https://github.com/bdoughan/blog20110819 /blob/master/pom.xml – 2013-03-12 11:27:30

+1

谢谢一吨杜恩:)工程很棒! – Arun 2013-03-12 11:56:35

0

JsonMarshaller是一个Java 1.5库,允许对Java对象进行编组和解组JSON对象。该项目的目标首先是易用性,透明度和静态类型安全。 例

如果你有以下的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; 
} 

,并创建了一个新的书(),并与信息填充它,它居然能够太:

JSON: 

{title: "java", 
isbn: "122333", 
authors: [{firstName: "Herbert", lastName: "Shield"}, 
      {firstName: "Pascal", lastName: "Perez"}]} 
+1

Thnksfor ur Response。但是我需要知道如何从上述类中获得JSON。 – Arun 2013-03-12 09:34:19

+1

查看文档https://code.google.com/p/jsonmarshaller/wiki/Introduction – 2013-03-12 10:56:27