在 Java 中将 JSON 转换为 XML
本教程介绍如何在 Java 中将 JSON 转换为 XML。
要将 JSON 转换为 XML,我们可以使用多个库,例如 org.json、下划线和 Jackson。在本文中,我们将学习使用这些库并查看 JSON 到 XML 的转换过程。
JSON 是应用程序用于数据交换的数据格式。由于轻量级和简单,它被应用程序使用,而 XML 是一种标记语言,也用于传输数据。
让我们从一些在 Java 中将 JSON 转换为 XML 的示例开始。
使用 Java 中的 org.json 库将 JSON 转换为 XML
在这个例子中,我们使用了 org.json 库,它提供了 JSONObject
和一个 XML 类。JSONObject 类用于将 JSON 字符串转换为 JSON 对象,然后通过使用 XML 类将 JSON 转换为 XML。
XML 类提供了一个静态的 toString() 方法,该方法将结果作为字符串返回。请参阅下面的示例。
import java.io.IOException;
import org.json.*;
public class SimpleTesting{
public static void main(String[] args) throws IOException, JSONException{
String jsonStr = "{student : { age:30, name : Kumar, technology : Java } }";
JSONObject json = new JSONObject(jsonStr);
String xml = XML.toString(json);
System.out.println(xml);
}
}
输出:
<student>
<name>Kumar</name>
<technology>Java</technology>
<age>30</age>
</student>
使用 Java 中的 underscore
库将 JSON 转换为 XML
在这里,我们使用 underscore
库将 JSON
转换为 XML
。我们使用了 U
类及其静态方法 jsonToXml()
,它以字符串形式返回 XML
。请参阅下面的示例。
import java.io.IOException;
import com.github.underscore.lodash.U;
public class SimpleTesting{
public static void main(String[] args) throws IOException{
String jsonStr = "{\"name\":\"JSON\",\"integer\":1,\"double\":2.0,\"boolean\":true,\"nested\":{\"id\":42},\"array\":[1,2,3]}";
System.out.println(jsonStr);
String xml = U.jsonToXml(jsonStr);
System.out.println(xml);
}
}
输出:
{"name":"JSON","integer":1,"double":2.0,"boolean":true,"nested":{"id":42},"array":[1,2,3]}
<?xml version="1.0" encoding="UTF-8"?>
<root>
<name>JSON</name>
<integer number="true">1</integer>
<double number="true">2.0</double>
<boolean boolean="true">true</boolean>
<nested>
<id number="true">42</id>
</nested>
<array number="true">1</array>
<array number="true">2</array>
<array number="true">3</array>
</root>
使用 Java 中的 Jackson 库将 JSON 转换为 XML
在这里,我们使用 ObjectMapper
类读取 JSON,然后使用 XmlMapper
类获取 XML 格式的数据。在这里,我们使用了两个主要的包,jackson.databind
和 jackson.dataformat
;第一个包含与 JSON 相关的类,第二个包含与 XML 相关的类。请参阅下面的示例。
import java.io.IOException;
import java.io.StringWriter;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
public class SimpleTesting{
public static void main(String[] args) throws IOException{
final String jsonStr = "{\"name\":\"JSON\",\"integer\":1,\"double\":2.0,\"boolean\":true,\"nested\":{\"id\":42},\"array\":[1,2,3]}";
ObjectMapper jsonMapper = new ObjectMapper();
JsonNode node = jsonMapper.readValue(jsonStr, JsonNode.class);
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_1_1, true);
StringWriter sw = new StringWriter();
xmlMapper.writeValue(sw, node);
System.out.println(sw.toString());
}
}
输出:
{"name":"JSON","integer":1,"double":2.0,"boolean":true,"nested":{"id":42},"array":[1,2,3]}
<?xml version="1.0" encoding="UTF-8"?>
<root>
<name>JSON</name>
<integer number="true">1</integer>
<double number="true">2.0</double>
<boolean boolean="true">true</boolean>
<nested>
<id number="true">42</id>
</nested>
<array number="true">1</array>
<array number="true">2</array>
<array number="true">3</array>
</root>
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Pandas 中执行 SQL 查询
发布时间:2024/04/24 浏览次数:1195 分类:Python
-
本教程演示了在 Python 中对 Pandas DataFrame 执行 SQL 查询。
在 Pandas 中使用 stack() 和 unstack() 函数重塑 DataFrame
发布时间:2024/04/24 浏览次数:1289 分类:Python
-
本文讨论了 Pandas 中 stack() 和 unstack() 函数的使用。
在 Pandas 中读取 Excel 多张工作表
发布时间:2024/04/24 浏览次数:1450 分类:Python
-
本教程演示如何在 Pandas Python 中从 Excel 工作簿中读取多个 Excel 工作表。