Sunday, 8 February 2015

Converting XML to JSON



Hi guys I am currently experimenting a little bit with JSON and XML and so i came across the following XML structure: (Which i took from the german wikipedia: http://ift.tt/1C8jW67)



<Kreditkarte
Herausgeber="Xema"
Nummer="1234-5678-9012-3456"
Deckung="2e+6"
Waehrung="EURO">
<Inhaber
Name="Mustermann"
Vorname="Max"
maennlich="true"
Alter="42"
Partner="null">
<Hobbys>
<Hobby>Reiten</Hobby>
<Hobby>Golfen</Hobby>
<Hobby>Lesen</Hobby>
</Hobbys>
<Kinder />
</Inhaber>
</Kreditkarte>


and the JSON i have found online looks like this:



{
"Herausgeber": "Xema",
"Nummer": "1234-5678-9012-3456",
"Deckung": 2e+6,
"Waehrung": "EURO",
"Inhaber": {
"Name": "Mustermann",
"Vorname": "Max",
"maennlich": true,
"Hobbys": [ "Reiten", "Golfen", "Lesen" ],
"Alter": 42,
"Kinder": [],
"Partner": null
}
}


BUT I would have done it this way:



{"Kreditkarte":
{
"herausgeber":"Xema",
"nummer":1234-5678-9012-3456,
"deckung":"2e+6",
"waehrung":"EURO",
"inhaber":
{
"name":"mustermann",
"vorname":"maxh",
"alter": 42,
"partner":null,
},
"hobbys":[
{"name":"reiten"},
{"name":"golfen"},
{"name":"lesen"}
],
"kinder":null
}
}


As you might see instead of enclosing three strings into the array "hobbys" i created three objects.


My questions is if the way i did it is wrong or if it is just different? And if it is ok, could you tell me what the advantages or disadvantages of the way i did it are?


Hope you do not mind that it is german!


Thank you in advance!


No comments:

Post a Comment