Tutorial :XML to JSON - losing root node



Question:

I'm using net.sf.json with a Java project and it works great.

The conversion of this XML:

<?xml version="1.0" encoding="UTF-8"?>  <important-data certified="true" processed="true">    <timestamp>232423423423</timestamp>    <authors>      <author>        <firstName>Tim</firstName>        <lastName>Leary</lastName>      </author>    </authors>    <title>Flashbacks</title>    <shippingWeight>1.4 pounds</shippingWeight>    <isbn>978-0874778700</isbn>  </important-data>  

converts to this in JSON:

{    "@certified": "true",    "@processed": "true",    "timestamp": "232423423423",    "authors": [  {      "firstName": "Tim",      "lastName": "Leary"    }],    "title": "Flashbacks",    "shippingWeight": "1.4 pounds",    "isbn": "978-0874778700"  }  

However, the root tag

<important-data>  

is lost in the conversion. Being new to XML and JSON, I am not sure if this is suppose to be the correct behaviour. If not, is there any way to tell net.sf.json to convert it while keeping the root node property?

Thanks.


Solution:1

It's the correct behaviour. The root tag represents the whole object. The root's attributes are converted in the fields of the JSON object.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »