json - Why does the DeserializeXmlNode output XML enclose array elements in an extra tag? -


i have following json converting xml:

{   "root": {     "object": [{       "name": "cust1",       "addresses": [         [{           "addressline1": "address1",           "addressline2": "address2"         }],         [{           "addressline1": "address3",           "addressline2": "address4"         }]       ]     }, {       "name": "cust2",       "addresses": [         [{           "addressline1": "address1_2",           "addressline2": "address2_2"         }],         [{           "addressline1": "address3_2",           "addressline2": "address4_2"         }]       ]     }]   } } 

i use following code convert xml (the root element has been added manually after json converted):

xmldocument xmldoc = jsonconvert.deserializexmlnode(rootjson); 

the output xml follows:

<root> <object>     <name>cust1</name>     <addresses>         <addresses>             <addressline1>address1</addressline1>             <addressline2>address2</addressline2>         </addresses>     </addresses>     <addresses>         <addresses>             <addressline1>address3</addressline1>             <addressline2>address4</addressline2>         </addresses>     </addresses> </object> <object>     <name>cust2</name>     <addresses>         <addresses>             <addressline1>address1_2</addressline1>             <addressline2>address2_2</addressline2>         </addresses>     </addresses>     <addresses>         <addresses>             <addressline1>address3_2</addressline1>             <addressline2>address4_2</addressline2>         </addresses>     </addresses> </object> 

the problem <addresses> tag followed <addresses> tag.

it's because have [ , ] below

  "addresses": [     [{       "addressline1": "address1",       "addressline2": "address2"     }],     [{       "addressline1": "address3",       "addressline2": "address4"     }]   ] 

if remove [ , ] below

  "addresses": [     {       "addressline1": "address1",       "addressline2": "address2"     },     {       "addressline1": "address3",       "addressline2": "address4"     }   ] 

your json this

{   "root": {     "object": [{       "name": "cust1",       "addresses": [         {           "addressline1": "address1",           "addressline2": "address2"         },         {           "addressline1": "address3",           "addressline2": "address4"         }       ]     }, {       "name": "cust2",       "addresses": [         {           "addressline1": "address1_2",           "addressline2": "address2_2"         },         {           "addressline1": "address3_2",           "addressline2": "address4_2"         }       ]     }]   } } 

and you'll following output xml

<root>   <object>     <name>cust1</name>     <addresses>       <addressline1>address1</addressline1>       <addressline2>address2</addressline2>     </addresses>     <addresses>       <addressline1>address3</addressline1>       <addressline2>address4</addressline2>     </addresses>   </object>   <object>     <name>cust2</name>     <addresses>       <addressline1>address1_2</addressline1>       <addressline2>address2_2</addressline2>     </addresses>     <addresses>       <addressline1>address3_2</addressline1>       <addressline2>address4_2</addressline2>     </addresses>   </object> </root> 

working fiddle: https://dotnetfiddle.net/oyj91s


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

Kivy: Swiping (Carousel & ScreenManager) -

jdbc - Not able to establish database connection in eclipse -