2

I use JSONObject's toJSONObject() to convert XML into JSON. However, there is an annoying converting problem for me.

For example, my original XML looks like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><testData><Timestamp>20170907005718936</Timestamp><Detail><Item1>02907005718520</Item1><Item2>02907425118520</Item2><Item3>20170921133613</Item3></Detail></testData>

then I do the following to convert them into JSON:

String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><testData><Timestamp>20170907005718936</Timestamp><Detail><Item1>02907005718520</Item1><Item2>02907425118520</Item2><Item3>20170921133613</Item3></Detail></testData>";
    try {
        JSONObject xmlObj = XML.toJSONObject(xml);
        System.out.println(xmlObj.toString());
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

After conversion, my data inside each tag is corrupted, because it thinks they are NUMBERS, not STRING.

{"testData":{"Timestamp":20170907005718936,"Detail":{"Item1":2907005718520,"Item2":2907425118520,"Item3":20170921133613}}}

How can I convert all tag values into String type only?

1
  • @CodeMatrix it's just "Converted", not into the data type I want Commented Sep 21, 2017 at 10:39

3 Answers 3

4

Try this,

JSONObject xmlObj = XML.toJSONObject(xml,true);

the boolean after the input XML in a function will return as you expected the result.

thanks, Morajse

Sign up to request clarification or add additional context in comments.

2 Comments

sadly this solution come late, so I'll give you thumb up for useful
@user6309529 you can change the accepted answer if a better one appears. See meta.stackoverflow.com/a/256595/721855
0

You can try convert every number to a String, something like:

String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><testData><Timestamp>20170907005718936</Timestamp><Detail><Item1>02907005718520</Item1><Item2>02907425118520</Item2><Item3>20170921133613</Item3></Detail></testData>";
xml = xml.replaceAll("(?<=\\d+)<", "\"<"); // any < preceded by digits
xml = xml.replaceAll(">(?=\\d+)", ">\""); // any > followed by digits
System.out.println(xml);

Output:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><testData><Timestamp>"20170907005718936"</Timestamp><Detail><Item1>"02907005718520"</Item1><Item2>"02907425118520"</Item2><Item3>"20170921133613"</Item3></Detail></testData>

EDIT:

If you may have String values that may start/end with a digit(s), you can specify the lower limit of how many digits your value should have to be considered as a Number, for example:

xml = xml.replaceAll("(?<=\\d{5})<", "\"<"); // any < preceded by at least 5 digits
xml = xml.replaceAll(">(?=\\d{5})", ">\""); // any > followed by at least 5 digits

3 Comments

this would add extra double-quote into my result if the value is STRING already. like <Item4>Item4</Item4> would be turned into "Item4":"Item4\"" after XML to JSON conversion. need to adjust it
But yours seem like most close to what I need. If there's no better one posted in next few days, I'll tag you as right answer.
Using string manipulation functions to transform JSON or XML is a bad practice and can cause issues, for example the extra quoting mentioned.
0

I think this can be use : Class :

public JSONObject() {
    this.map = new LinkedHashMap<String, Object>();
}
 
 
public JSONObject(Map<String, Object> map) {
    this.map = new LinkedHashMap<String, Object>();
    if (map != null) {
        Iterator<Entry<String, Object>> i = map.entrySet().iterator();
        while (i.hasNext()) {
            Entry<String, Object> entry = i.next();
            Object value = entry.getValue();
            if (value != null) {
                this.map.put(entry.getKey(), wrap(value));
            }
        }
    }
}

Then on your main :

String xml = FileUtils.readFileToString(new File("test.xml"));
System.out.println(org.json.XML.toJSONObject(xml).toString());

Hope it help's you ! =)

1 Comment

sadly this one make no different between using XML.toJSONObject(), my 0 on the left at each tag value still trimmed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.