5

I would like to convert XML to JSON.

Currently, I make this with the lib org.json :

JSONObject jso = XML.toJSONObject(xmlStr);

However, if the XML contains number fields, I would like to have only String fields in the JSONObject.

For example:

The XML file is :

<ID>3</ID>
<NAME>ApplicationName</NAME>

The org.json permits me to have:

{
    "ID" : 3,
    "Name" : "ApplicationName"
}

The final result has to be :

{
    "ID" : "3",
    "Name" : "ApplicationName"
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
J.Canonne
  • 121
  • 1
  • 5
  • That's a tough one. You might have more success deserializing the XML to a Map to get everything to be String and then serializing that to JSON. Can't say for certain – jseashell Dec 19 '16 at 15:15
  • Thanks. But it's possible to deserialize the XML file when it is more complexe than the exemple ? – J.Canonne Dec 19 '16 at 15:27
  • I don't see why not. As long as it's a JSON Object and not a JSON Array, using the Map should be fine – jseashell Dec 19 '16 at 15:35

1 Answers1

7

I resolve mt problem by using the latest version of org.json.

There is a methode to do this :

JSONObject jso = XML.toJSONObject(xmlStr, true);

The boolean is using to keep the string fields.

J.Canonne
  • 121
  • 1
  • 5
  • which version is this one accept the boolean flag ? can you plese mentioned here ? we couldnt find in the latest version – Azhagu Surya Mar 01 '21 at 01:36