简体   繁体   English

如何使用 gson 库将 xml 转换为 json

[英]How to convert an xml into a json using gson library

I have to convert a XML using gson library into a JSON我必须使用 gson 库将 XML 转换为 JSON

i haven´t found how to do it using gson library(java)我还没有找到如何使用 gson 库(java)

You could use Jackson to do this:你可以使用杰克逊来做到这一点:

import these libraries:导入这些库:

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

then do this in your class:然后在你的课堂上这样做:

    public class Example {
        private String name;
        private int number;
    }
    Example example = new XmlMapper().readValue(xml, Example.class);
    String json = new ObjectMapper().writeValueAsString(example);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM