如何仅使用 Jackson 将 XML 转换为 JSON? [英] How to convert XML to JSON using only Jackson?

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

问题描述

我收到来自服务器的 XML 响应.但我需要以 JSON 格式显示.

I am getting a response from server as XML. But I need to display this in JSON format.

有没有办法在没有任何第三方 API 的情况下转换它?我使用了 Jackson,但为此我需要创建 POJO.

Is there any way to convert it without any third party API? I used Jackson but for this I need to create POJO.

来自服务器的响应是这样的:

The response from server is like this:

<?xml version='1.0'?>
<errors><error><status>400</status><message>The field 'quantity' is invalid.</message><details><invalid_reason>The quantity specified is greater than the quantity of the product that is available to ship.</invalid_reason><available_quantity>0</available_quantity><order_product_id>12525</order_product_id></details></error></errors>

推荐答案

使用 Jackson 2.x

您可以使用 Jackson 做到这一点,并且不需要 POJO:

Using Jackson 2.x

You can do that with Jackson and no POJOs are required for that:

String xml = "<?xml version="1.0" encoding="UTF-8"?>
" +
             "<errors>
" +
             "  <error>
" +
             "    <status>400</status>
" +
             "    <message>The field 'quantity' is invalid.</message>
" +
             "    <details>
" +
             "      <invalid_reason>The quantity specified is greater than the quantity of the product that is available to ship.</invalid_reason>
" +
             "      <available_quantity>0</available_quantity>
" +
             "      <order_product_id>12525</order_product_id>
" +
             "    </details>
" +
             "  </error>
" +
             "</errors>";

XmlMapper xmlMapper = new XmlMapper();
JsonNode node = xmlMapper.readTree(xml.getBytes());

ObjectMapper jsonMapper = new ObjectMapper();
String json = jsonMapper.writeValueAsString(node);

需要以下依赖项:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.8.2</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.8.2</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.8.2</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
    <version>2.8.2</version>
</dependency>

注意文档中规定的XmlMapper限制一个>:

Be aware of the XmlMapper limitations stated in the documentation:

树模型仅以有限的方式支持:具体来说,Java数组和Collections可以写入,但不能读取,因为无法区分数组和没有附加信息的对象.

Tree Model is only supported in limited fashion: specifically, Java arrays and Collections can be written, but can not be read, since it is not possible to distinguish Arrays and Objects without additional information.

正如 Jackson 作者在评论中Jackson 2.12 终于 改进了 XML 处理,以便使用 JsonNodeObject 作为目标类型,if 保留重复项.

As nicely highlighted by the Jackson author in the comments, Jackson 2.12 finally improved XML handling, so that duplicates are preserved if using JsonNode or Object as target types.

您也可以使用 JSON.org 来实现:

You also can do it with JSON.org:

String xml = "<?xml version="1.0" encoding="UTF-8"?>
" +
             "<errors>
" +
             "  <error>
" +
             "    <status>400</status>
" +
             "    <message>The field 'quantity' is invalid.</message>
" +
             "    <details>
" +
             "      <invalid_reason>The quantity specified is greater than the quantity of the product that is available to ship.</invalid_reason>
" +
             "      <available_quantity>0</available_quantity>
" +
             "      <order_product_id>12525</order_product_id>
" +
             "    </details>
" +
             "  </error>
" +
             "</errors>";

String json = XML.toJSONObject(xml).toString();

需要以下依赖项:

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20160810</version>
</dependency>

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

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