1

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?

1 Answer 1

1

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

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

Comments

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.