在 Java 中将 JSON 转换为 XML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19977979/
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
Converting JSON to XML in Java
提问by vinod
I am new to json. I am having a program to generate xml from json object.
我是 json 的新手。我有一个程序可以从 json 对象生成 xml。
String str = "{'name':'JSON','integer':1,'double':2.0,'boolean':true,'nested':{'id':42},'array':[1,2,3]}";
JSON json = JSONSerializer.toJSON( str );
XMLSerializer xmlSerializer = new XMLSerializer();
xmlSerializer.setTypeHintsCompatibility( false );
String xml = xmlSerializer.write( json );
System.out.println(xml);
the output is:
输出是:
<?xml version="1.0" encoding="UTF-8"?>
<o><array json_class="array"><e json_type="number">1</e><e json_type="number">2</e><e json_type="number">3</e></array><boolean json_type="boolean">true</boolean><double json_type="number">2.0</double><integer json_type="number">1</integer><name json_type="string">JSON</name><nested json_class="object"><id json_type="number">42</id></nested></o>
my biggest problem is how to write my own attributes instead of json_type="number" and also writing my own sub elements like .
我最大的问题是如何编写我自己的属性而不是 json_type="number" 以及如何编写我自己的子元素,如 .
采纳答案by Bruno Grieder
Use the (excellent) JSON-Java library from json.org then
使用来自 json.org 的(优秀的)JSON-Java 库然后
JSONObject json = new JSONObject(str);
String xml = XML.toString(json);
toString
can take a second argument to provide the name of the XML root node.
toString
可以采用第二个参数来提供 XML 根节点的名称。
This library is also able to convert XML to JSON using XML.toJSONObject(java.lang.String string)
该库还能够使用以下方法将 XML 转换为 JSON XML.toJSONObject(java.lang.String string)
Check the Javadoc
检查Javadoc
Link to the the github repository
链接到github 存储库
POM
聚甲醛
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160212</version>
</dependency>
original post updated with new links
原始帖子更新了新链接
回答by Piyush Chordia
If you have a valid dtd file for the xml then you can easily transform json to xml and xml to json using the eclipselink jar binary.
如果您有 xml 的有效 dtd 文件,那么您可以使用 eclipselink jar 二进制文件轻松地将 json 转换为 xml 并将 xml 转换为 json。
Refer this: http://www.cubicrace.com/2015/06/How-to-convert-XML-to-JSON-format.html
请参阅:http: //www.cubicrace.com/2015/06/How-to-convert-XML-to-JSON-format.html
The article also has a sample project (including the supporting third party jars) as a zip file which can be downloaded for reference purpose.
文章还有一个示例项目(包括支持的第三方jars)zip文件,可以下载以供参考。
回答by Saurabh
If you want to replace any node value you can do like this
如果你想替换任何节点值,你可以这样做
JSONObject json = new JSONObject(str);
String xml = XML.toString(json);
xml.replace("old value", "new value");
回答by Martynas Jusevi?ius
Transforming with XSLT 3.0 is the only proper way to do it, as far as I can tell. It is guaranteed to produce valid XML, and a nice structure at that. https://www.w3.org/TR/xslt/#json
据我所知,使用 XSLT 3.0 进行转换是唯一正确的方法。它保证生成有效的 XML,并且是一个很好的结构。 https://www.w3.org/TR/xslt/#json
回答by Valentyn Kolesnikov
Underscore-javalibrary has static method U.jsonToXml(jsonstring)
. I am the maintainer of the project. Live example
Underscore-java库有静态方法U.jsonToXml(jsonstring)
。我是项目的维护者。活生生的例子
import com.github.underscore.lodash.U;
public class MyClass {
public static void main(String args[]) {
String json = "{\"name\":\"JSON\",\"integer\":1,\"double\":2.0,\"boolean\":true,\"nested\":{\"id\":42},\"array\":[1,2,3]}";
System.out.println(json);
String xml = U.jsonToXml(json);
System.out.println(xml);
}
}
Output:
输出:
{"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>