【问题标题】:Convert XML to/from JSON in Java (without extra <e> and <o> elements)在 Java 中将 XML 转换为 JSON(没有额外的 <e> 和 <o> 元素)
【发布时间】:2011-09-19 14:10:02
【问题描述】:

我正在使用 json-lib 库中的 XMLSerializer,以便在 JSON 和 XML 之间来回转换。

有没有办法避免生成&lt;e&gt;&lt;a&gt; 节点?这很不方便被路径表达式破坏?

考虑下面的例子:

{"store": {
  "book":   [
        {
      "category": "reference",
      "author": "Nigel Rees",
      "title": "Sayings of the Century",
      "price": 8.95
    },
        {
      "category": "fiction",
      "author": "Evelyn Waugh",
      "title": "Sword of Honour",
      "price": 12.99
    },
        {
      "category": "fiction",
      "author": "Herman Melville",
      "title": "Moby Dick",
      "isbn": "0-553-21311-3",
      "price": 8.99
    },
        {
      "category": "fiction",
      "author": "J. R. R. Tolkien",
      "title": "The Lord of the Rings",
      "isbn": "0-395-19395-8",
      "price": 22.99
    }
  ],
  "bicycle":   {
    "color": "red",
    "price": 19.95
  }
}}

被序列化为:

<?xml version="1.0" encoding="UTF-8"?>
<o>
  <store class="object">
    <bicycle class="object">
      <color type="string">red</color>
      <price type="number">19.95</price>
    </bicycle>
    <book class="array">
      <e class="object">
        <author type="string">Nigel Rees</author>
        <category type="string">reference</category>
        <price type="number">8.95</price>
        <title type="string">Sayings of the Century</title>
      </e>
      <e class="object">
        <author type="string">Evelyn Waugh</author>
        <category type="string">fiction</category>
        <price type="number">12.99</price>
        <title type="string">Sword of Honour</title>
      </e>
      <e class="object">
        <author type="string">Herman Melville</author>
        <category type="string">fiction</category>
        <isbn type="string">0-553-21311-3</isbn>
        <price type="number">8.99</price>
        <title type="string">Moby Dick</title>
      </e>
      <e class="object">
        <author type="string">J. R. R. Tolkien</author>
        <category type="string">fiction</category>
        <isbn type="string">0-395-19395-8</isbn>
        <price type="number">22.99</price>
        <title type="string">The Lord of the Rings</title>
      </e>
    </book>
  </store>
</o>

http://jsontoxml.utilities-online.info/ 提供的东西正是我所追求的,但似乎在 Java 中不可用。

【问题讨论】:

    标签: java xml json


    【解决方案1】:

    您可以通过 StAX 使用 StAXON - JSON 来执行此操作。 StAXON 实现 XML 的 Streaming API,但读取和写入 JSON。也就是说,您可以使用标准 XML 工具来完成这项工作。

    复制 XML 的一个常见“技巧”是使用身份 XML 转换 (XSLT)。

    这里有一些代码:

    InputStream input = ... // your JSON input;
    OutputStream output = System.out;
    try {
        /*
         * Create source.
         */
        XMLInputFactory inputFactory = new JsonXMLInputFactory();
        inputFactory.setProperty(JsonXMLInputFactory.PROP_MULTIPLE_PI, false);
        Source source = new StAXSource(inputFactory.createXMLStreamReader(input));
    
        /*
         * Create result.
         */
        XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
        XMLStreamWriter writer = outputFactory.createXMLStreamWriter(output);
        writer = new PrettyXMLStreamWriter(writer); // format output
        Result result = new StAXResult(writer);
    
        /*
         * Transform (copy).
         */
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.newTransformer().transform(source, result);
    } finally {
        /*
         * As per StAX specification, XMLStreamReader/Writer.close() doesn't close
         * the underlying stream.
         */
        output.close();
        input.close();
    }
    

    用你的例子运行它会产生:

    <?xml version="1.0" ?>
    <store>
        <book>
            <category>reference</category>
            <author>Nigel Rees</author>
            <title>Sayings of the Century</title>
            <price>8.95</price>
        </book>
        <book>
            <category>fiction</category>
            <author>Evelyn Waugh</author>
            <title>Sword of Honour</title>
            <price>12.99</price>
        </book>
        <book>
            <category>fiction</category>
            <author>Herman Melville</author>
            <title>Moby Dick</title>
            <isbn>0-553-21311-3</isbn>
            <price>8.99</price>
        </book>
        <book>
            <category>fiction</category>
            <author>J. R. R. Tolkien</author>
            <title>The Lord of the Rings</title>
            <isbn>0-395-19395-8</isbn>
            <price>22.99</price>
        </book>
        <bicycle>
            <color>red</color>
            <price>19.95</price>
        </bicycle>
    </store>
    

    注意:如果输出属性PROP_MULTIPLE_PI设置为true,StAXON会生成&lt;xml-multiple&gt;处理指令。在转换回 JSON 以触发数组启动时,StAXON 可以使用这些。如果您不需要返回 JSON,请将此设置保留为 false

    【讨论】:

    • @-chris 要使用哪个版本的 stAXON 和输入流,我们可以将文件名作为输入或仅提供字符串和 Result result = new StAXResult(writer);这里显示将 Result 转换为 staxresult 的错误并且输出不正确
    • @user1182090 使用 staxon-1.0。您可以使用任何 Reader/InputStream 和 Writer/OutputStream(例如 FileReader/Writer)。如果您对 StAXResult 有问题,您也可以使用 StAX 事件 API。见github.com/beckchr/staxon/wiki/Converting-JSON-to-XML
    • @-chris,您的代码工作正常,但现在我正在从 json 文件中读取并使用您的代码创建 xml,但是如何将有效的 json 文件转换为 obj 并从该 obj 我应该打印 xml...
    • @user1182090 看看 JAXB。您也可以使用 StAXON 执行此操作,请参阅 github.com/beckchr/staxon/wiki/Using-JAXB。无论如何,这超出了这个问题的范围。
    • 如果我有一个 xml 5 并且想要转换为 json 对象,如“errorCode”:“5”(不是:5),如何强制它不是号码?
    猜你喜欢
    • 2020-04-15
    • 1970-01-01
    • 2014-01-13
    • 2013-11-27
    • 2013-06-01
    • 1970-01-01
    • 2018-05-22
    • 2014-11-19
    相关资源
    最近更新 更多