【问题标题】:Converting JSON to XML in Java在 Java 中将 JSON 转换为 XML
【发布时间】:2013-11-27 11:35:01
【问题描述】:

我是 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" 以及如何编写自己的子元素,例如 .

【问题讨论】:

  • vinod,你有没有解决上述问题的解决方案,“将你自己的属性写入从json生成的XML标签”。我也在寻找解决方案,如果你发现了,请在这个帖子中发布。提前致谢。
  • @Malleswari 您可以将 JSON 转换为地图,修改它然后再转换回来。

标签: java xml json xpath


【解决方案1】:

Underscore-java 库有静态方法U.jsonToXml(jsonstring)Live example

import com.github.underscore.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>

【讨论】:

  • 感谢这个。尤其是混合 json 对象数组结构,它比评分最高的答案容易得多
  • @Valentyn - 如何在将 Json 转换为 XML 时添加 soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
  • 谢谢。这是 2020 年的最佳解决方案。其他一些库不起作用。
【解决方案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);
ObjectWriter ow = xmlMapper.writer().withRootName("root");
StringWriter w = new StringWriter();
ow.writeValue(w, node);
System.out.println(w.toString());

打印:

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

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

【讨论】:

  • 你最好展示如何覆盖“ObjectNode”。
  • @SimonLogic 好提示!添加了“功能”。
【解决方案3】:

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

【讨论】:

【解决方案4】:

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

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

【讨论】:

    【解决方案5】:

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

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

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

    【讨论】:

      【解决方案6】:

      然后使用来自 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>
      

      用新链接更新了原始帖子

      【讨论】:

      • 谢谢。如何编写 json 对象以将属性添加到 等元素
      • 我想唯一的方法是将 JSON 反序列化为(自定义)Java 对象。然后,使用 XMLBeans 或 XStream 等框架并借助注解,指定哪个属性作为元素,哪个作为属性
      • 或者,使用上面简单两行中的 XML 并对其应用 XSLT
      • 非常方便简单的一对一 JSON 到 XML 的转换。
      • 这对我不起作用,我从有效的 json 中得到了无效的 xml。
      猜你喜欢
      • 2013-06-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-23
      • 2013-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多