Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
425 views
in Technique[技术] by (71.8m points)

java - How to convert XML to JSON using only Jackson?

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

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

The following dependencies are required:

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

Be aware of the XmlMapper limitations stated in the documentation:

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.

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.

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

The following dependency is required:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...