Reputation: 9512
I'm trying to convert an XML string to a JSON object. I used net.sf.json, but it strips away the root element.
XMLSerializer xmlSerializer = new XMLSerializer();
net.sf.json.JSON netsfJSON = xmlSerializer.read(xml);
I also tried org.json.XML
JSONObject jsonObject=XML.toJSONObject(xml);
but it doesn't differentiate the attributes from the elements. Is there anything else I can use or are there some settings in the two libraries above?
Upvotes: 1
Views: 1979
Reputation: 11
After createing the XMLSerializer
object you need to set the forceTopLevelObject property to true due to the fact that the default setting is false.
XMLSerializer xmlSerializer = new XMLSerializer();
xmlSerializer.setForceTopLevelObject(true);
Hope this helps.
Carol
Upvotes: 1