如何仅使用Jackson将XML转换为JSON? [英] How to convert XML to JSON using only Jackson?
问题描述
我从服务器获得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\"?>\n" +
"<errors>\n" +
" <error>\n" +
" <status>400</status>\n" +
" <message>The field 'quantity' is invalid.</message>\n" +
" <details>\n" +
" <invalid_reason>The quantity specified is greater than the quantity of the product that is available to ship.</invalid_reason>\n" +
" <available_quantity>0</available_quantity>\n" +
" <order_product_id>12525</order_product_id>\n" +
" </details>\n" +
" </error>\n" +
"</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
中所述的限制文档:
仅以有限的方式支持树模型:具体而言,可以编写Java数组和
集合
,但无法读取,因为在没有附加信息的情况下无法区分数组和对象。
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.
使用JSON.org
您也可以使用JSON.org:
Using JSON.org
You also can do it with JSON.org:
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<errors>\n" +
" <error>\n" +
" <status>400</status>\n" +
" <message>The field 'quantity' is invalid.</message>\n" +
" <details>\n" +
" <invalid_reason>The quantity specified is greater than the quantity of the product that is available to ship.</invalid_reason>\n" +
" <available_quantity>0</available_quantity>\n" +
" <order_product_id>12525</order_product_id>\n" +
" </details>\n" +
" </error>\n" +
"</errors>";
String json = XML.toJSONObject(xml).toString();
需要以下依赖项:
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160810</version>
</dependency>
这篇关于如何仅使用Jackson将XML转换为JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!