【问题标题】:XML to json conversion, in Java, while preserving the data typesXML 到 json 的转换,在 Java 中,同时保留数据类型
【发布时间】:2012-01-10 09:00:40
【问题描述】:

我正在使用org.json api 将 xml 转换为 json。问题是我的 xml 中的某些标签包含非字符串值(即 int、boolean、double 等......)。

我在下面使用的代码成功地将 xml 转换为 json,但原始数据类型在 json 响应中转换为字符串。例如:xml 中的 <age>10<age> 被转换为 {"age" : "10" },其中 json 中的值 10String,应该是 Integer。例如{"age" : 10 }

我为转换提供的 xml 输入是动态的,并且会经常更改。但为了将其显示为示例,我将 xml 保存为String。动态生成的 xml 代码将保存在字符串变量中。

谁能帮助我将 xml 转换为 json,同时保留数据类型?下面是代码

import org.json.*;
public class XmlToJson {
     private static String xmlCode = 
                     "<handler>
                         <price>10</price>
                         <item>rice</item>
                         <VALUE>3434</VALUE>
                     </handler>
                     <flow>
                     </flow>";
         public static void main(String[] args) throws JSONException{
               JSONObject xmlJsonObj=null;
               xmlJsonObj = XML.toJSONObject(xmlCode);
               System.out.println(xmlJsonObj.toString(1));
         }
}

示例输出:

{
    "flow": "",
    "handler": {
        "VALUE": "3434",
        "item": "rice",
        "price": "10"
    }
}

预期的行为是:

{
    "flow": "",
    "handler": {
        "VALUE": 3434,
        "item": "rice",
        "price": 10
    }
}

【问题讨论】:

  • 我想知道代码怎么知道10是一个int而不是一个字符串
  • 我明白了。但是有没有办法解析转换后的 json 以搜索原始数据类型并将它们转换回其原始类型?我的要求是...... :-)
  • 我放弃尝试格式化,那是你覆盖格式化的 2 次。

标签: java xml json


【解决方案1】:

你应该看看我的开源库unXml,它解析xml,输出Json。它是为 Java 8 编写的,并使用(很棒的)JacksonJson-library。

UnXml 在 Maven Central 上可用。

鉴于此 xml(添加了根以使其格式正确)

<root>
    <handler>
        <price>10</price>
        <item>rice</item>
        <VALUE>3434</VALUE>
    </handler>
    <flow></flow>
</root>

你创建一个这样的解析器

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.nerdforge.unxml.Parsing;
import com.nerdforge.unxml.factory.ParsingFactory;
import com.nerdforge.unxml.parsers.Parser;
import org.w3c.dom.Document;

public class XmlParser {
    public ObjectNode parseXml(String inputXml){
        Parsing parsing = ParsingFactory.getInstance().create();
        Document document = parsing.xml().document(inputXml);

        Parser<ObjectNode> parser = parsing.obj("root")
            .attribute("flow")
            .attribute("handler",  "handler", parsing.obj()
                .attribute("price", "price", parsing.number())
                .attribute("item")
                .attribute("value", "VALUE", parsing.with(Integer::parseInt))
            )
            .build();

        ObjectNode result = parser.apply(document);
        return result;
    }
}

哪个会返回这个 Json

{
    "handler":{
        "item":"rice",
        "price":10.0,
        "value":3434
    },
    "flow":""
}

【讨论】:

    【解决方案2】:

    如果您的架构已设置,您可以通过 POJO 进行数据绑定(将 XML 转换为 JSO, 将 POJO 转换为 JSON)——有很多数据绑定工具,我会推荐那些在拉模型之上工作的工具(XStream 用于 XML,GSON 用于 JSON——但有丰富的选择 挑选你最喜欢的)

    如果您在 XML 方面做得更好,您还可以编写 XSLT 转换,它将 XML 转换为您喜欢的任何内容,也可以转换为 JSON - 尽管我个人认为 JavaBeans 更容易

    【讨论】:

      【解决方案3】:

      我赞同 Konstantin 的建议,即在 XML 和 JSON 之间使用 POJO——否则,由于两者之间的不匹配,您将遇到无穷无尽的问题。具体来说,XML 具有表达数组/列表的“本机”方式,因此处理空和单元素列表/数组通常会产生奇怪的结果。

      我的建议是对 JSON 使用 Jackson,对 XML 使用 JAXBJackson XML-data-binding -- 这样,如果必须更改命名,您还可以使用 JAXB 注释获取额外信息。

      不管你用什么,转储 org.json 包;现在替代品更好,因此包装主要用作历史遗物(当没有替代品时它很有用)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-03
        • 2018-07-23
        • 2016-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多