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?