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"
}
3
  • That's a tough one. You might have more success deserializing the XML to a Map<String, String> to get everything to be String and then serializing that to JSON. Can't say for certain Commented Dec 19, 2016 at 15:15
  • Thanks. But it's possible to deserialize the XML file when it is more complexe than the exemple ? Commented Dec 19, 2016 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<String, String> should be fine Commented Dec 19, 2016 at 15:35

1 Answer 1

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.

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

1 Comment

which version is this one accept the boolean flag ? can you plese mentioned here ? we couldnt find in the latest version

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.