Michał Niklas
Michał Niklas

Reputation: 54322

Converting from XML to JSON using org.json: how to preserve number format?

I am converting simple XML to JSON using org.json lib:

import org.json.JSONObject;
import org.json.XML;

public class TestJson {
    public static void test_number() {
        String xml = "<BALANCE>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_number();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

My results:

<BALANCE>32032033.10</BALANCE>
----
{"BALANCE": 3.20320331E7}

As you see the format of number changed from normal, to scientific. How can I preserve number format? I would like to see JSON as:

{"BALANCE": 32032033.10}

or preferable as string:

{"BALANCE": "32032033.10"}

I use org.json from json-20190722.jar

Upvotes: 0

Views: 3784

Answers (3)

MOnkey
MOnkey

Reputation: 841

Here you could use either

 <!-- https://mvnrepository.com/artifact/org.json/json -->
 <dependency>
      <groupId>org.json</groupId>
      <artifactId>json</artifactId>
      <version>20140107</version>
 </dependency>

Then your existing code will work without any change.

Otherwise you want to go with the latest version i.e. json-20190722.jar, then you need to pass true as an extra parameter in your toJSONObject(String string, boolean keepStrings) method as suggested by @vigneshwaran m.

where @param keepStrings If true, then values will not be coerced into boolean
     *  or numeric values and will instead be left as strings

This is from json-20190722.jar javadoc, check here https://github.com/stleary/JSON-java/blob/master/XML.java

Upvotes: 1

vigneshwaran m
vigneshwaran m

Reputation: 376

Hope the below code will help to resolve the issue. Please use XML.toJSONObject(xml,true) instead of XML.toJSONObject(xml);

import org.json.JSONObject;
import org.json.XML;

public class TestJson {
    public static void test_number() {
        String xml = "<BALANCE>32032033.10</BALANCE>";
        JSONObject jsonObj = XML.toJSONObject(xml,true);
        System.out.println(String.format("%s\n----\n%s", xml, jsonObj.toString()));
    }

    public static void main(String[] args) {
        try {
            test_number();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Output:

<BALANCE>32032033.10</BALANCE>
----
{"BALANCE":"32032033.10"}

Upvotes: 3

deHaar
deHaar

Reputation: 18578

You have a String that you get by parsing the JSON using this line:

String json_str = jsonObj.toString(4);

You can preserve or reset the format by creating a BigDecimal from that String and applying the number formatting using setScale(...) like this:

public static void main(String[] args) {
    String json_str = "3.20320331E7";
    BigDecimal bigDecimal = new BigDecimal(json_str);
    System.out.println(bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP));
}

The output of that example main is this:

32032033.10

Upvotes: 0

Related Questions