在Java中将JSON转换为XML

vin*_*nod 51 java xml xpath json

我是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); 
Run Code Online (Sandbox Code Playgroud)

输出是:

<?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>
Run Code Online (Sandbox Code Playgroud)

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

Bru*_*der 102

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

JSONObject json = new JSONObject(str);
String xml = XML.toString(json);
Run Code Online (Sandbox Code Playgroud)

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

该库还可以使用XML将XML转换为JSON XML.toJSONObject(java.lang.String string)

检查Javadoc

链接到github存储库

POM

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20160212</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

原帖更新了新链接

  • 这对我不起作用,我从有效的 json 中得到无效的 xml。 (2认同)

Val*_*kov 7

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); 
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

{"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>
Run Code Online (Sandbox Code Playgroud)


jsc*_*sse 7

对于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);
ObjectWriter ow = xmlMapper.writer().withRootName("root");
StringWriter w = new StringWriter();
ow.writeValue(w, node);
System.out.println(w.toString());
Run Code Online (Sandbox Code Playgroud)

印刷:

<?xml version='1.1' encoding='UTF-8'?>
<root>
  <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>
</root>
Run Code Online (Sandbox Code Playgroud)

要将其转换回来(xml 到 json),请查看此答案/sf/answers/4372826881/