Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert xml to json without conversion String/Integer?

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"
}
like image 459
J.Canonne Avatar asked Dec 19 '16 15:12

J.Canonne


People also ask

How to convert XML data to JSON format?

It helps to convert your XML data to JSON format. This tool allows loading the XML URL, which loads XML and converts to String. Click on the URL button, Enter URL and Submit. Users can also convert XML File to JSON by uploading the file. Once you are done with XML to JSON converting. You can download as a file or Create a link and Share.

What happens to the integer in XML when converted to string?

The Integer in XML is converted as a String, while you expect it to be still an Integer. Read more... SAP Cloud Platform Integration for process services

Is it required to save and share josn to XML?

XML to JSON is very unique tool for convert JOSN to XML and allows to download, save, share and print XML to JSON data.. XML to JSON support URL linking for sharing json. No. It's not required to save and share code. Please send us the details of the URL using Contact US.

How to covert numbers to string in the output JSON format?

This is of the boolean data type. If you want to covert all the numbers to string in the output JSON structure, then set this variable as ‘true’. Setting this to true will result in all the numbers (integer, float, long, short, double) to be in double-quotes in the output JSON format. Default value is ‘false’. How to set the parameter value?


1 Answers

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.

like image 69
J.Canonne Avatar answered Oct 02 '22 11:10

J.Canonne