2013-02-24 222 views
2

我想将xml转换为json。使用jackson将xml转换为json

XML的格式如下 -

<default> 
     <column>      
     <title>Title 1</title> 
    <id>id1</id> 
    <value>val1</value> 
    </column> 
    <column> 
    <title>Title 2</title> 
    <id>id2</id> 
    <value>val2</value> 
    </column> 
    <column> 
    <title>Title 3</title> 
    <id>id3</id> 
    <value>val3</value> 
    </column> 
    </default> 

和转换后我期待下面的JSON -

{ 
    "column": [ 
     { 
      "title": "Title 1", 
      "id": "id1", 
      "value": "val1" 
     }, 
     { 
      "title": "Title 2", 
      "id": "id2", 
      "value": "val2" 
     }, 
     { 
      "title": "Title 3", 
      "id": "id3", 
      "value": "val3" 
     } 
    ] 
} 

但是当我使用杰克逊为此,它给了我下面的JSON -

{ 
    "column": { 
     "title": "Title 3", 
     "id": "id3", 
     "value": "val3" 
    } 
} 

我试过使用杰克逊1.9和杰克逊2.1,但它没有给我预期的产出。

有人可以让我知道是否有可能或者我需要更改我的XML格式? 下面是我写acheive上述情况下的代码 -

try { 
      XmlMapper xmlMapper = new XmlMapper(); 
      Map entries = xmlMapper.readValue(new File("xmlPath"), Map.class); 

      ObjectMapper jsonMapper = new ObjectMapper(); 
      String json = jsonMapper.writeValueAsString(entries); 
      System.out.println(json); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     }  

感谢

+0

它看起来像刚刚过去的元素正在转换。您应该为您的问题添加一个[SSCCE](http://sscce.org/),说明您如何执行此转换。 – Perception 2013-02-25 15:36:30

+0

你在做什么_exactly_?你的描述只是谈论预期的输入,输出,而不是你正在做什么(尝试)来得到。 – StaxMan 2013-02-25 19:23:51

回答

0

虽然在一组项目中,我遇到了你所描述的相同问题的工作。我们用来解决这个问题的解决方案是将我们的Maven依赖项从Jackson更改为json-lib。我们使用这个网站作为指南:http://answers.oreilly.com/topic/278-how-to-convert-xml-to-json-in-java/

<dependency> 
    <groupId>net.sf.json-lib</groupId> 
    <artifactId>json-lib</artifactId> 
    <version>2.4</version> 
    <type>jar</type> 
    <classifier>jdk15</classifier> 
    <scope>compile</scope> 
</dependency> 

- $ pli7

+0

链接已损坏。 – divine 2017-11-07 09:11:07

0

我也遇到了这个问题,用杰克逊2.5。我的解决办法是用我自己的,这时候一个新的对象添加行为就像它来取代的javax.json.JsonObjectBuilder执行中使用:

public class CustomJsonObjectBuilder implements JsonObjectBuilder { 

    private final Map<String, JsonValue> jsonFragments; 

    // other methods omitted for brevity 

    public JsonObjectBuilder add(String name, JsonValue value) { 
    if(jsonFragments.containsKey(name)) { 
     JsonValue oldValue = jsonFragments.get(name); 
     JsonArrayBuilder newBuilder = new JsonArrayBuilderImpl(); 
     if(oldValue instanceof JsonArray) { 
      JsonArray theArray = (JsonArray) oldValue; 
      for (JsonObject oldValues : theArray.getValuesAs(JsonObject.class)) { 
       newBuilder.add(oldValues); 
      } 
     } else { 
      newBuilder.add(oldValue); 
     } 
     newBuilder.add(value); 
     jsonFragments.put(name, newBuilder.build()); 
    } else { 
     jsonFragments.put(name, value); 
    } 
    return this; 
    } 
1

我能够通过使用org.json API转换源得到解决这个问题XML到JSONObject,然后由Jackson API提供给JSON。

代码

import java.io.File; 
import java.io.FileInputStream; 
import java.io.InputStream; 

import org.apache.commons.io.IOUtils; 
import org.json.JSONObject; 
import org.json.XML; 

import com.fasterxml.jackson.databind.ObjectMapper; 
import com.fasterxml.jackson.databind.SerializationFeature; 

... 
... 

try (InputStream inputStream = new FileInputStream(new File(
       "source.xml"))) { 
    String xml = IOUtils.toString(inputStream); 
    JSONObject jObject = XML.toJSONObject(xml); 
    ObjectMapper mapper = new ObjectMapper(); 
    mapper.enable(SerializationFeature.INDENT_OUTPUT); 
    Object json = mapper.readValue(jObject.toString(), Object.class); 
    String output = mapper.writeValueAsString(json); 
    System.out.println(output); 
} 

... 
...