在Java中将JSON转换为XML

时间:2020-11-27 22:18:15

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",以及如何编写自己的子元素,比如。

5 个解决方案

#1


80  

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. tojsonobject (java.lang)将XML转换为JSON。字符串的字符串)

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

原来的帖子更新了新的链接。

#2


4  

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.

本文还将示例项目(包括支持的第三方jar)作为zip文件下载,以供参考。

#3


0  

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

#4


0  

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

#5


0  

There is underscore-java library with static methods fromJson and toXml. Live example.

有使用json和toXml的静态方法的underscore-java库。生活的例子。

Code example:

代码示例:

import com.github.underscore.lodash.$;
import java.util.Map;

public class MyClass {
    @SuppressWarnings("unchecked")
    public static void main(String args[]) {
        String str = "{\"name\":\"JSON\",\"integer\":1,\"double\":2.0,\"boolean\":true,\"nested\":{\"id\":42},\"array\":[1,2,3]}";  
        Map<String, Object> json = (Map<String, Object>) $.fromJson( str );  
        System.out.println(json); 
        String xml = $.toXml(json);  
        System.out.println(xml); 
    }
}

#1


80  

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. tojsonobject (java.lang)将XML转换为JSON。字符串的字符串)

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

原来的帖子更新了新的链接。

#2


4  

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.

本文还将示例项目(包括支持的第三方jar)作为zip文件下载,以供参考。

#3


0  

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

#4


0  

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

#5


0  

There is underscore-java library with static methods fromJson and toXml. Live example.

有使用json和toXml的静态方法的underscore-java库。生活的例子。

Code example:

代码示例:

import com.github.underscore.lodash.$;
import java.util.Map;

public class MyClass {
    @SuppressWarnings("unchecked")
    public static void main(String args[]) {
        String str = "{\"name\":\"JSON\",\"integer\":1,\"double\":2.0,\"boolean\":true,\"nested\":{\"id\":42},\"array\":[1,2,3]}";  
        Map<String, Object> json = (Map<String, Object>) $.fromJson( str );  
        System.out.println(json); 
        String xml = $.toXml(json);  
        System.out.println(xml); 
    }
}