I need to convert a json to xml and later on converting that json back to xml, but i am loosing the json array object while this conversion - I am using org.json lib.
Json String -
{
"readResult": {
"errors": [{
"code": 400
}]
}
}
Codebase - using org.json lib
String xml = XML.toString(new JSONObject("inputjsonstring"));
String json = XML.toJSONObject(xml).toString();
Output xml and json -
XML - <readResult><errors><code>400</code></errors></readResult>
JSON -
{
"readResult": {
"errors": {
"code": 400
}
}
}
Here this json doesn't have any array as it was in original json. Please suggest alternate library to do the same.
There is no way that from the XML string
, the library can "guess" that
codeis an arrayTo do these kind of things, you will need to go via a Java model and use a Library such as Jackson to regenerate the JSON
There are several alternatives to map from XML to Java: XStream is one of them