2

我想将 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 格式?以下是我为实现上述场景而编写的代码 -

    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();
        }       

谢谢

4

3 回答 3

2

通过使用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);
}

...
...
于 2016-04-07T19:06:38.353 回答
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

于 2013-05-13T21:07:37.017 回答
0

我也遇到了这个问题,使用杰克逊 2.5。我的解决方案是javax.json.JsonObjectBuilder用我自己的替换 in use 的实现,当添加新对象时,它的行为如下:

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;
   }
于 2015-06-25T14:48:27.913 回答