2013-10-24 89 views
2

我有這個類:XML包裝JSON格式問題

class A { 
     @XmlElement(name = "bees") 
     @XmlElementWrapper(name="bee") 
     public List<B> bees; 
    } 

然後我的XML看起來像:

<a> 
    <bees> 
    <bee>...</bee> 
    <bee>...</bee> 
    </bees> 
</a> 

然而,當通過JSON使用:

{ 
    "bees": { 
    "bee": [ 
     .... 
    ] 
    } 
} 

我需要成爲:

  { 
    "bees": { 
    "bee": { .. }, "bee": { .. }, .. 
    } 
} 

任何人都可以幫忙嗎?

回答

0

喜歡的東西結束:使用大型XML文件時

{ 
    "bees": { 
    "bee": { .. }, "bee": { .. }, .. 
    } 
1

使用此tools;我遇到了你所描述的同樣的問題。我們用來解決這個問題的解決方案是將我們的Maven依賴項從Jackson更改爲json-lib。我們使用這個網站作爲guide

如果使用maven,則將依賴項添加到您的pom中。

<dependency> 
     <groupId>net.sf.json-lib</groupId> 
     <artifactId>json-lib</artifactId> 
     <version>2.3</version> 
     <type>jar</type> 
     <classifier>jdk15</classifier> 
     <scope>compile</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.commons</groupId> 
     <artifactId>commons-io</artifactId> 
     <version>1.3.2</version> 
     <type>jar</type> 
     <scope>compile</scope> 
    </dependency> 
    <dependency> 
     <groupId>xom</groupId> 
     <artifactId>xom</artifactId> 
     <version>1.1</version> 
    </dependency> 
    </dependencies> 

請將這個XML樣本文件樣本xml.xml

<?xml version="1.0" encoding="UTF-8"?> 
    <important-data certified="true" processed="true"> 
     <timestamp>232423423423</timestamp> 
     <authors> 
     <author> 
      <firstName>Tim</firstName> 
      <lastName>Leary</lastName> 
     </author> 
     </authors> 
     <title>Flashbacks</title> 
     <shippingWeight>1.4 pounds</shippingWeight> 
     <isbn>978-0874778700</isbn> 
    </important-data> 

主要類

package com.discursive.answers; 

import java.io.InputStream; 

import net.sf.json.JSON; 
import net.sf.json.xml.XMLSerializer; 

import org.apache.commons.io.IOUtils; 

public class ConvertXMLtoJSON { 

     public static void main(String[] args) throws Exception { 
       InputStream is = 
         ConvertXMLtoJSON.class.getResourceAsStream("sample-xml.xml"); 
       String xml = IOUtils.toString(is); 

       XMLSerializer xmlSerializer = new XMLSerializer(); 
       JSON json = xmlSerializer.read(xml); 
       System.out.println(json.toString(2)); 
     } 
} 

結果

{ 
    "@certified": "true", 
    "@processed": "true", 
    "timestamp": "232423423423", 
    "authors": [ { 
    "firstName": "Tim", 
    "lastName": "Leary" 
    }], 
    "title": "Flashbacks", 
    "shippingWeight": "1.4 pounds", 
    "isbn": "978-0874778700" 
} 
+0

Java堆空間錯誤:

public class BeeDetails { public string details { get; set; } } public class Bee { public BeeDetails { get; set; } } public class BeeResponse { public List<Bee> Bees { get; set; } } 

得到這個。感謝您的幫助 – mel3kings

3

注:我是EclipseLink JAXB (MOXy)負責人和JAXB (JSR-222)專家組成員。

相反的:

{ 
    "bees": { 
    "bee": { .. }, 
    "bee": { .. }, 
    .. 
    } 
} 

我想提出以下建議:

{ 
    "bees" : [ 
     { .. }, 
     { .. }, 
     .. 
    } ] 
} 

下面是莫西可以如何使用基於您的映射產生這種JSON:

import java.util.*; 
import javax.xml.bind.*; 
import javax.xml.transform.stream.StreamSource; 
import org.eclipse.persistence.jaxb.JAXBContextProperties; 
import org.eclipse.persistence.jaxb.MarshallerProperties; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     Map<String, Object> properties = new HashMap<String, Object>(); 
     properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false); 
     properties.put(JAXBContextProperties.JSON_WRAPPER_AS_ARRAY_NAME, true); 
     JAXBContext jc = JAXBContext.newInstance(new Class[] {A.class}, properties); 

     Unmarshaller unmarshaller = jc.createUnmarshaller(); 
     StreamSource xml = new StreamSource("src/forum19560166/input.xml"); 
     A a = unmarshaller.unmarshal(xml, A.class).getValue(); 

     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json"); 
     marshaller.marshal(a, System.out); 
    } 

} 

欲瞭解更多信息