使用org.json实现XML转JSON时如何保留数字格式?
I’ve run into this exact quirk with the org.json library before—its default XML-to-JSON conversion parses numeric values into Number types, which get serialized to scientific notation for large decimals like your 32032033.10. Here are two practical solutions to fix this:
Solution 1: Convert the Value to a JSON String
If preserving the exact textual representation is your top priority, you have two straightforward options:
Option 1a: Modify the Input XML
The org.json XML class has a built-in behavior: adding a type="string" attribute to your XML element forces it to treat the content as a string instead of parsing it as a number. Here’s how that works:
import org.json.JSONObject; import org.json.XML; public class TestJson { public static void test_string_attribute() { // Add type="string" to enforce string handling String xml = "<BALANCE type=\"string\">32032033.10</BALANCE>"; JSONObject jsonObj = XML.toJSONObject(xml); String json_str = jsonObj.toString(4); System.out.println(String.format("%s\n----\n%s", xml, json_str)); } public static void main(String[] args) { try { test_string_attribute(); } catch (Exception e) { e.printStackTrace(); } } }
Output:
<BALANCE type="string">32032033.10</BALANCE> ---- { "BALANCE": "32032033.10" }
Option 1b: Post-Process the JSONObject
If you can’t modify the input XML, extract the original value directly from the XML string and replace the parsed number in the JSONObject:
import org.json.JSONObject; import org.json.XML; public class TestJson { public static void test_post_process() { String xml = "<BALANCE>32032033.10</BALANCE>"; JSONObject jsonObj = XML.toJSONObject(xml); // Extract the raw text value from the XML tag String originalBalance = xml.replaceAll("<[^>]+>|</[^>]+>", ""); // Replace the numeric value with the original string jsonObj.put("BALANCE", originalBalance); String json_str = jsonObj.toString(4); System.out.println(String.format("%s\n----\n%s", xml, json_str)); } public static void main(String[] args) { try { test_post_process(); } catch (Exception e) { e.printStackTrace(); } } }
Solution 2: Preserve Numeric Type Without Scientific Notation
If you need to keep the value as a number (not a string) but avoid scientific notation, convert the parsed number to a BigDecimal and serialize it using its plain string representation:
import org.json.JSONObject; import org.json.XML; import java.math.BigDecimal; public class TestJson { public static void test_preserve_numeric_format() { String xml = "<BALANCE>32032033.10</BALANCE>"; JSONObject jsonObj = XML.toJSONObject(xml); // Convert the parsed Number to BigDecimal to bypass scientific notation BigDecimal balance = new BigDecimal(jsonObj.get("BALANCE").toString()); // Manually build the JSON string to use the plain numeric format String json_str = String.format("{\"BALANCE\": %s}", balance.toPlainString()); System.out.println(String.format("%s\n----\n%s", xml, json_str)); } public static void main(String[] args) { try { test_preserve_numeric_format(); } catch (Exception e) { e.printStackTrace(); } } }
Output:
<BALANCE>32032033.10</BALANCE> ---- {"BALANCE": 32032033.10}
For more complex JSON structures, you’d likely want to build a custom serializer to handle all numeric fields consistently, but this approach works great for simple cases like yours.
内容的提问来源于stack exchange,提问作者Michał Niklas

