在Java中将JSON转换为XML

时间:2013-11-14 12:43:43

标签: java xml json xpath

我是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); 

输出是:

<?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>

我最大的问题是如何编写自己的属性而不是json_type =“number”,还要编写自己的子元素。

6 个答案:

答案 0 :(得分:90)

使用json.org的(优秀)JSON-Java库,然后

JSONObject json = new JSONObject(str);
String xml = XML.toString(json);

toString可以使用第二个参数来提供XML根节点的名称。

此库还可以使用XML.toJSONObject(java.lang.String string)

将XML转换为JSON

检查Javadoc

链接到the github repository

POM

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

使用新链接更新原始帖子

答案 1 :(得分:4)

如果您有一个有效的xml dtd文件,那么您可以使用eclipselink jar二进制文件轻松地将json转换为xml,将xml转换为json。

请参阅:http://www.cubicrace.com/2015/06/How-to-convert-XML-to-JSON-format.html

该文章还有一个示例项目(包括支持的第三方罐子)作为zip文件,可以下载以供参考。

答案 2 :(得分:2)

对于从json到xml ,请使用下面的Jackson示例:

final String str = "{\"name\":\"JSON\",\"integer\":1,\"double\":2.0,\"boolean\":true,\"nested\":{\"id\":42},\"array\":[1,2,3]}";
ObjectMapper jsonMapper = new ObjectMapper();
JsonNode node = jsonMapper.readValue(str, 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 w = new StringWriter();
xmlMapper.writeValue(w, node);
System.out.println(w.toString());

打印:

<?xml version='1.1' encoding='UTF-8'?>
<ObjectNode>
  <name>JSON</name>
  <integer>1</integer>
  <double>2.0</double>
  <boolean>true</boolean>
  <nested>
    <id>42</id>
  </nested>
  <array>1</array>
  <array>2</array>
  <array>3</array>
</ObjectNode>

要将其转换回( xml到json ),请查看此答案https://stackoverflow.com/a/62468955/1485527

答案 3 :(得分:0)

如果要替换任何节点值,可以这样做

JSONObject json = new JSONObject(str);
String xml = XML.toString(json);
xml.replace("old value", "new value");

答案 4 :(得分:0)

据我所知,使用XSLT 3.0进行转换是唯一正确的方法。它保证生成有效的XML,并且具有良好的结构。 https://www.w3.org/TR/xslt/#json

答案 5 :(得分:0)

underscore-java库,静态方法为U.jsonToXml(jsonstring)。我是该项目的维护者。 Live example

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

输出:

{"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>