How to convert XML to JSON using only Jackson?

后端 未结 6 1713
无人共我
无人共我 2020-11-30 04:35

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

相关标签:
6条回答
  • 2020-11-30 04:42

    I have just followed cassiomolin steps. I have faced below exception while using the jackson 2.x libraries.

    Cannot create XMLStreamReader or XMLEventReader from a org.codehaus.stax2.io.Stax2ByteArraySource
    

    If you also face the above exception. Add the below code to fix the issue. Then you can able to see the converted JSON without the namespace.

    JacksonXmlModule module = new JacksonXmlModule();
    module.setDefaultUseWrapper(false);
    XmlMapper xmlMapper = new XmlMapper(module);
    

    Tips : If you want to convert the XML without the namespace then use jackson library. Dont go for org.json libs. It doesnt support this use case.

    0 讨论(0)
  • 2020-11-30 04:44

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

    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.

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

    The following dependency is required:

    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20160810</version>
    </dependency>
    
    0 讨论(0)
  • 2020-11-30 04:45

    Using jackson

    import com.fasterxml.jackson.dataformat.xml.XmlMapper;
    import java.io.IOException;
    
    public class JsonUtil {
    
        private static XmlMapper XML_MAPPER = new XmlMapper();
        private static ObjectMapper JSON_MAPPER = new ObjectMapper();
    
        public static ObjectMapper getJsonMapper(){
            return JSON_MAPPER;
        }
    
        public static XmlMapper getXmlMapper(){
            return XML_MAPPER;
        }
    
        public static String xmlToJson(String xml){
            try {
                return getJsonMapper().writeValueAsString(getXmlMapper().readTree(xml));
            } catch (IOException e) {
                e.printStackTrace();
                return "";
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-30 04:46

    Is there any way to convert xml to json without using any third party API?

    If you are being practical, no there isn't.

    The step of parsing the XML can be performed using APIs that are part of Java SE. However going from the parsed XML (e.g. a DOM) to JSON requires a JSON support library, and Java SE does not include one.

    (In theory you could write such a library yourself, but what is the point of doing that?)


    I used Jackson but for this I need to create POJO.

    @Cassio points out that Jackson allows you to do this translation without writing POJOs. Alternatively, look at other (3rd-party) JSON APIs for Java; see http://www.json.org for a list of alternatives. Some of the simpler ones don't involve defining POJOs

    0 讨论(0)
  • 2020-11-30 04:56

    I know that I am too late for an answer here. But I am writing this for the new guys who stumbled upon this question and thinking to use @Cassio's answer.

    The problem of using XmlMpper to de-serialize to a JsonNode is that, when there are multiple elements with the same name at the same level, then it will replace the previous one with the new one and end up with data loss. Usually, we've to add this to an array. To tackle this problem, we can override the _handleDuplicateField() method of the JsonNodeDeserializer class. Enough talking. Let's see the code

    public class DuplicateToArrayJsonNodeDeserializer extends JsonNodeDeserializer {
    
        @Override
        protected void _handleDuplicateField(JsonParser p, DeserializationContext ctxt, 
            JsonNodeFactory nodeFactory,String fieldName, ObjectNode objectNode,
            JsonNode oldValue, JsonNode newValue) throws JsonProcessingException {
            ArrayNode node;
            if(oldValue instanceof ArrayNode){
                node = (ArrayNode) oldValue;
                node.add(newValue);
            } else {
                node = nodeFactory.arrayNode();
                node.add(oldValue);
                node.add(newValue);
            }
            objectNode.set(fieldName, node);
        }
    }
    

    Since we've overridden the default deserializer, we also need to register this in the XmlMapper to make it work.

    XmlMapper xmlMapper = new XmlMapper();
    xmlMapper.registerModule(new SimpleModule().addDeserializer(
        JsonNode.class, 
        new DuplicateToArrayJsonNodeDeserializer()
    ));
    JsonNode node = xmlMapper.readTree(payLoad);
    
    0 讨论(0)
  • 2020-11-30 05:06
    package com.src.test;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URL;
    
    import org.apache.commons.io.IOUtils;
    
    import net.sf.json.JSON;
    import net.sf.json.xml.XMLSerializer;
    
    public class JSONConverter {
        private static URL url = null;
        private static InputStream input = null;
    
        public static void main(String args[]) throws IOException {
            try {
                url = JSONConverter.class.getClassLoader().getResource("sampleXmlFilePath.xml");
                input = url.openStream();
                String xmlData = IOUtils.toString(input);
    
                XMLSerializer xmlSerializer = new XMLSerializer();
                JSON json = xmlSerializer.read(xmlData);
                System.out.println("JSON format : " + json);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                input.close();
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题