简体   繁体   English

如何在 Java 中将 XML 转换为 JSON 并避免解析器尝试将字符串解析为数字

[英]How to convert XML to JSON in Java and avoid the parser to attempt parsing String as numbers

I'm using org.json.XML library to parse XML to JSON.我正在使用 org.json.XML 库将 XML 解析为 JSON。 http://www.json.org/javadoc/org/json/XML.html http://www.json.org/javadoc/org/json/XML.html

In my XML doc there is an ID field that is randomly generated with [0-9][az].在我的 XML 文档中有一个使用 [0-9][az] 随机生成的 ID 字段。 It is intended to be a String.它旨在成为一个字符串。 Everything works fine until there's this unlucky ID 123456789e1234 that happens to be a scientific notation of a number.一切正常,直到出现这个不幸的 ID 123456789e1234,它恰好是数字的科学记数法。 Here's a piece of test code:这是一段测试代码:

public class XmlToJsonTest {
    public static String testXML = "<MyXML><ID>123456789e1234</ID></MyXML>";
    @Test
    public void testXMLtoJSON() throws JSONException {
        JSONObject testJsonObject = XML.toJSONObject(testXML);
    }
}

Here's the exception:这是例外:

org.json.JSONException: JSON does not allow non-finite numbers.

The XML lib toJson() method first attempts to converts the String to Integer, Long or Double and if none works, it gives up parsing it as number and treats it like a String. XML lib toJson() 方法首先尝试将 String 转换为 Integer、Long 或 Double,如果无效,则放弃将其解析为数字并将其视为字符串。 In this case the String 123456789e1234 is parsable as a Double.在这种情况下,字符串 123456789e1234 可解析为 Double。 And later when the lib checks if the double is infinite by Double.isInfinite(), it throws the JSONException since obviously 123456789e1234 is greater than Double's standard of finite.稍后,当 lib 通过 Double.isInfinite() 检查 double 是否为无限时,它会抛出 JSONException,因为显然 123456789e1234 大于 Double 的有限标准。

How do I force it not to parse the value as number at all?如何强制它根本不将值解析为数字? Is there any java library out there that correctly converts XML to JSON in this case?在这种情况下,是否有任何 Java 库可以将 XML 正确转换为 JSON?

Try this ...尝试这个 ...

import org.json.me.JSONObject;

import cjm.component.cb.json.ToJSON;

public class DummyClass
{
public static void main(String[] args)
{
    try
    {
        String xml = "<MyXML><ID>123456789e1234</ID></MyXML>";

        JSONObject obj = (new ToJSON().convertToJSON(xml));

        System.out.println(obj);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}
}

Output输出

 -------- XML Detected -------- 
 -------- JSON created Successfully -------- 
{"MyXML":{"ID":"123456789e1234"}}

Get the Conversion Box Jar for that ....获取转换盒罐....

Not sure, but this works for me:不确定,但这对我有用:

String testXML = "<MyXML><ID>123456789e1234</ID></MyXML>";
JSONObject testJsonObject = org.json.XML.toJSONObject(testXML);
System.out.println(testJsonObject.getJSONObject("MyXML").getString("ID"));

There is an underscore-java library with static method U.xmlToJson(xml) .有一个带有静态方法U.xmlToJson(xml)underscore-java库。 I am the maintainer of the project.我是项目的维护者。

public class XmlToJsonTest {
    public static String testXML = "<MyXML><ID>123456789e1234</ID></MyXML>";

    public void main(String[] args) {
        String testJson = U.xmlToJson(testXML);
        System.out.println(testJson);
    }
}

Output:输出:

{
  "MyXML": {
    "ID": "123456789e1234"
  },
  "#omit-xml-declaration": "yes"
}

If you need the XML to be parsed as String, you can pass the second parameter to XML.toJSONObject as XMLParserConfiguration.KEEP_STRINGS如果您需要将 XML 解析为字符串,则可以将第二个参数作为XMLParserConfiguration.KEEP_STRINGS传递给XML.toJSONObject

public static void main(String[] args) {
    System.out.println(XML.toJSONObject("<MyXML><ID>123456789e1234</ID></MyXML>", XMLParserConfiguration.KEEP_STRINGS));
}

Output输出

{"MyXML":{"ID":"123456789e1234"}}
    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM