2013-03-12 45 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