Xml data lost in json conversion

I get the following result from converting xml to JSON using more than one conversion library. As you can see, the attributes of the property name are lost, as are the attributes of the element name. Why?

Does anyone have any recommendations on how I can modify my XML to make it more convenient to convert?

<Asset name="xyz">
  <Property name="p1">Value 1</Property> 
  <Property name="p2">Value 2</Property> 
  <TimeSeries name="TimeSeries Name 1">
    <Item name="30 Apr 2009">97.47219</Item> 
    <Item name="01 May 2009">97.16496</Item> 
    <Item name="05 May 2009">97.34606</Item> 
  </TimeSeries>
</Asset>

Retuns

{"Asset":{"@attributes":{"name":"xyz"},
"Property":["Value 1","Value 2"],
"TimeSeries":{"@attributes":{"name":"TimeSeries Name 1"},
"Item":["97.47219","97.16496","97.34606"]}}}

I tried the following, but both XML and JSON are much more verbose:

<Asset name="xyz">
  <Property><name>p1</name><value>Value 1</value></Property> 
  <Property><name>p2</name><value>Value 2</value></Property> 
  <TimeSeries name="TimeSeries Name 1">
      <Item><date>30 Apr 2009</date><value>97.47219</value></Item> 
      <Item><date>01 May 2009</date><value>97.16496</value></Item> 
      <Item><date>05 May 2009</date><value>97.34606</value></Item> 
  </TimeSeries>
</Asset>

as a result...

{"Asset":{"@attributes":{"name":"xyz"},
"Property":[{"name":"p1","value":"Value 1"},{"name":"p2","value":"Value 2"}],
"TimeSeries":{"@attributes":{"name":"TimeSeries Name 1"},
    "Item":[{"date":"30 Apr 2009","value":"97.47219"},
    {"date":"01 May 2009","value":"97.16496"},{"date":"05 May 2009","value":"97.34606"}]}}}
+3
source share
1 answer

You should probably never use attributes in the source XML file if you use this conversion tool.

, , , . ASP.NET , #, JSON, ​​ DataContractJsonSerializer -. JSON - .asmx, ContentType JSON? JSON AJAX WebService? .

+1

Source: https://habr.com/ru/post/1744510/


All Articles