Converting XML to JSON skipping root element



I have to convert xml to JSON.I have implemented in below way



XMLSerializer xmlSerializer = new XMLSerializer();
//xmlSerializer.setForceTopLevelObject(true);
//Above line is comented since i want to skip xml root element
JSON json = xmlSerializer.read(inputXml);
outJSON = json.toString(2);


My input xml is



<details>
<name>
<firstName>sir</firstName>
<lastName>John</lastName>
<midddleName>Nash</midddleName>
</name>
<address>24525 ross street</address>
</details>


My output JSON is which is coming fine skipping root the element



{
"name": {
"firstName": "sir",
"lastName": "John",
"midddleName": "Nash"
},
"address": "24525 ross street"
}


But when i gave request as



<details>
<name>
<firstName>sir</firstName>
<lastName>John</lastName>
<midddleName>Nash</midddleName>
</name>
</details>


My JSON response is



[
{
"firstName": "sir",
"lastName": "John",
"midddleName": "Nash"
}
]


But i need response as


{ "name": { "firstName": "sir", "lastName": "John", "midddleName": "Nash" } }


Please suggest me even if there any other API to provide this functionality


Any help would be appreciated.


Thanks


No comments:

Post a Comment