在 Java 中将 XML 转换为 JSON 的最快方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1823264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:59:44  来源:igfitidea点击:

Quickest way to convert XML to JSON in Java

javaxmljson

提问by BeachRunnerFred

What are some good tools for quickly and easily converting XML to JSON in Java?

有哪些好的工具可以在 Java 中快速轻松地将 XML 转换为 JSON?

回答by danieltalsky

JSON in Javahas some great resources.

Java中的JSON有一些很好的资源。

Maven dependency:

Maven 依赖:

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

XML.javais the class you're looking for:

XML.java是你要找的班级:

import org.json.JSONObject;
import org.json.XML;

public class Main {

    public static int PRETTY_PRINT_INDENT_FACTOR = 4;
    public static String TEST_XML_STRING =
        "<?xml version=\"1.0\" ?><test attrib=\"moretest\">Turn this to JSON</test>";

    public static void main(String[] args) {
        try {
            JSONObject xmlJSONObj = XML.toJSONObject(TEST_XML_STRING);
            String jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);
            System.out.println(jsonPrettyPrintString);
        } catch (JSONException je) {
            System.out.println(je.toString());
        }
    }
}

Output is:

输出是:

{"test": {
    "attrib": "moretest",
    "content": "Turn this to JSON"
}}

回答by Martin Sturm

I don't know what your exact problem is, but if you're receiving XML and want to return JSON (or something) you could also look at JAX-B. This is a standard for marshalling/unmarshalling Java POJO's to XML and/or Json. There are multiple libraries that implement JAX-B, for example Apache's CXF.

我不知道您的确切问题是什么,但是如果您收到 XML 并想返回 JSON(或其他内容),您也可以查看 JAX-B。这是将 Java POJO 编组/解组为 XML 和/或 Json 的标准。有多个实现 JAX-B 的库,例如 Apache 的 CXF。

回答by Marcus

The only problem with JSON in Javais that if your XML has a single child, but is an array, it will convert it to an object instead of an array. This can cause problems if you dynamically always convert from XML to JSON, where if your example XML has only one element, you return an object, but if it has 2+, you return an array, which can cause parsing issues for people using the JSON.

Java 中 JSON的唯一问题是,如果您的 XML 有一个孩子,但它是一个数组,它会将其转换为对象而不是数组。如果您总是动态地从 XML 转换为 JSON,这可能会导致问题,如果您的示例 XML 只有一个元素,则返回一个对象,但如果它有 2+ 个元素,则返回一个数组,这可能会导致使用JSON。

Infoscoop's XML2JSONclass has a way of tagging elements that are arrays before doing the conversion, so that arrays can be properly mapped, even if there is only one child in the XML.

Infoscoop 的XML2JSON类有一种在进行转换之前标记数组元素的方法,这样即使 XML 中只有一个子元素,也可以正确映射数组。

Here is an exampleof using it (in a slightly different language, but you can also see how arrays is used from the nodelist2json() method of the XML2JSON link).

这是一个使用它的示例(使用稍微不同的语言,但您也可以从 XML2JSON 链接的 nodelist2json() 方法中了解如何使用数组)。

回答by Krishna

To convert XML File in to JSON include the following dependency

要将 XML 文件转换为 JSON 包括以下依赖项

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

and you can Download Jar from Maven Repository here. Then implement as:

你可以在这里从 Maven 存储库下载 Jar。然后实现为:

String soapmessageString = "<xml>yourStringURLorFILE</xml>";
JSONObject soapDatainJsonObject = XML.toJSONObject(soapmessageString);
System.out.println(soapDatainJsonObject);

回答by pareshm

I have uploaded the project you can directly open in eclipse and run that's all https://github.com/pareshmutha/XMLToJsonConverterUsingJAVA

我已经上传了你可以在eclipse中直接打开并运行的项目 https://github.com/pareshmutha/XMLToJsonConverterUsingJAVA

Thank You

谢谢你

回答by horizon7

I found this the quick and easy way: Used: org.json.XML class from java-json.jar

我发现这是一种快速简便的方法:Used: org.json.XML class from java-json.jar

        if (statusCode == 200 && inputStream != null) {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            StringBuilder responseStrBuilder = new StringBuilder();

            String inputStr;
            while ((inputStr = bufferedReader.readLine()) != null) {
                responseStrBuilder.append(inputStr);
            }

            jsonObject = XML.toJSONObject(responseStrBuilder.toString());
        }