使用jackson将xml转换为json [英] Converting xml to json using jackson

查看:233
本文介绍了使用jackson将xml转换为json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将xml转换为json。

I want to convert an xml to json.

xml的格式如下 -

The format of of xml is as follows -

 <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 -

And after the conversion i am expecting following json -

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



<但是,当我为此目的使用杰克逊时,它让我跟随json -

But when i use jackson for this purpose it gives me following json -

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

我尝试过使用jackson 1.9和jackson 2.1,但它并没有给我预期的输出。

I have tried using jackson 1.9 and jackson 2.1, but it didnt gave me the expected output.

有人可以告诉我,不管我是可能的还是我需要更改我的xml格式?
以下是我为实现上述情况而编写的代码 -

Can some one please let me know that whether it is possible or i need to change my xml format ? Following is the code that i have written to acheive the above scenario -

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

谢谢

推荐答案

我能够通过使用 org.json API将源XML转换为 JSONObject 然后由Jackson API发送给JSON。

I was able to get the solution to this problem by using org.json API to convert source XML to JSONObject and then to JSON by Jackson API.

代码

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

...
...

这篇关于使用jackson将xml转换为json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆