2014-10-07 200 views
0

我一直在使用JAXB對象。現在我的要求是使用JSON而不是XML。有人能幫我解決這個問題嗎?我可以將JSON對象轉換爲JAXB對象嗎?

我無法讀取用json對象替換整個代碼庫。有什麼辦法將這些json轉換成jaxb對象嗎?

在此先感謝。

XML:

<skins> 
    <skin id="lineNormal" type="style" displayname="Skin"> 
    <attributes datatype="string" value="one" name="bg_type" platform="general"/> 
    <attributes datatype="string" value="5c5c5c00" name="background_color" platform="general"/> 
    <attributes datatype="boolean" value="false" name="customcss" platform="general"/> 
    <attributes datatype="string" value="00000000" name="border_color" platform="general"/> 
    <attributes datatype="number" value="0" name="border_width" platform="general"/> 
    <attributes datatype="string" value="plain" name="border_style" platform="general"/> 
    <attributes datatype="number" value="0" name="border_radius" platform="general"/> 
    <attributes datatype="string" value="Line" name="wtype" platform="general"/> 
</skin> 

JSON:

{ 
    "lineNormal": { 
    "wType": "Button", 
    "bg_type": "one", 
    "border_color": "00000000", 
    "border_radius": 0, 
    "border_style": "plain", 
    "border_width": 0, 
    "border_type": 0, 
    "font_color": "00000000", 
    "font_size": 100, 
    "font_weight": "normal", 
    "background_color : "xxx" 
    } 
} 

用於從上述獲取的XML應用圖表我的類文件是: '

@XmlRootElement(name = "application") 
public class Application implements Serializable 
{ 
    private ArrayList <Container> containers; 
    private Skins skins = new Skins(); 
    private String name; 
    private String id; 
    @XmlElement(name = "container") 
    public ArrayList <Container> getContainers() { 
     return containers; 
    } 

    /** 
    * Sets teh containers collection 
    * @param containers 
    */ 
    public void setContainers(ArrayList <Container> containers) { 
     this.containers = containers; 
    } 

    /** 
    * 
    * @return 
    */ 
    @XmlElement(name = "skins") 
    public Skins getSkins() 
    { 
     return skins; 
    } 

    /** 
    * 
    * @param skins 
    */ 
    public void setSkins(Skins skins) 
    { 
     this.skins = skins; 
    } 

    @XmlElement(name = "globals") 
    public Global getGlobal() { 
     return global; 
    } 

    public void setGlobal(Global global) { 
     this.global = global; 
    } 



    @XmlAttribute(name="id") 
    public String getId() { 
     return id; 
    } 

    public void setId(String id) { 
     this.id = id; 
    } 

    @XmlElement(name = "attributes") 
    public ArrayList<Attributes> getAttributes() { 
     return attributes; 
    }` 

//皮膚類是: : `

public class Skins implements Serializable{ 

    private ArrayList <Skin> skins = new ArrayList(); 

    @XmlElement(name = "skin") 
    public ArrayList<Skin> getSkinList() 
    { 
     return skins; 
    } 

    /** 
    * 
    * @param skins 
    */ 
    public void setSkinList(ArrayList<Skin> skins) 
    { 
     this.skins = skins; 
    } 

} 

`

回答

1

「我可以轉換成JSON對象JAXB對象?」

jackson-module-jaxb-annotations對於Jackson將允許你這樣做。

這個傑克遜擴展模塊提供了對使用JAXB(javax.xml.bind)註釋替代本機Jackson註釋的支持。它最常用於使重用與JAXB框架一起使用的現有數據bean更容易讀取和寫入XML。

與Maven,你只需要這個依賴(這將在其他幾個傑克遜的核心依賴拉):

<dependency> 
    <groupId>com.fasterxml.jackson.module</groupId> 
    <artifactId>jackson-module-jaxb-annotations</artifactId> 
    <version>2.4.0</version> 
</dependency> 

有了這種依賴性,你只需要註冊與ObjectMapper

的JAXB模塊
JaxbAnnotationModule module = new JaxbAnnotationModule(); 
ObjectMapper objectMapper = new ObjectMapper(); 
objectMapper.registerModule(module); 

然後,您可以將JSON讀入JAXB對象。

RootObject root = objectMapper.readValue(json, RootObject.class); 

由於模塊不支持所有JAXB註釋,因此這不是所有用例的必然解決方案。見here for supported annotations


+0

但問題是我沒有特定的ID名稱和all.I只是skin1,其數據,skin2及其數據等等.skin1 skin2 ..是不同的id。這個實現在任何地方都找不到。請你幫我實現這個 – honey123 2014-10-08 04:49:14

+0

我們至少需要看看你正在使用的JAXB類,或從一個xsd我們可以生成JAXB類 – 2014-10-08 05:25:51

+0

這裏是我的一個類文件。 @XmlRootElement(name =「application」) public class Application實現Serializable Private ArrayList containers; private skins skins = new Skins(); @XmlElement(name =「container」) public ArrayList getContainers(){ return containers; } //這樣的設置器和獲取器 }和皮膚是用於檢索所有皮膚的另一類,並且爲每個單獨的皮膚提供皮膚類 – honey123 2014-10-08 05:31:49