tags:

views:

47

answers:

1

I have this JSON:

{
  animals:{
      dog:[
          {
              name:'Rufus',
              breed:'labrador'
          },
          {
              name:'Marty',
              breed:'whippet'
          }
      ],
      cat:{
          name:'Matilda'
      }
  }
}

Why is it that it translates to this:

<animals>
    <dog>
        <name>Rufus</name>
        <breed>labrador</breed>
    </dog>
    <dog>
        <name>Marty</name>
        <breed>whippet</breed>
    </dog>
    <cat name="Matilda"/>
</animals>

and not this:

<animals>
    <dog name = "Rufus" breed = "labrador"/>
    <dog name = "Marty" breed = "whippet"/>
    <cat name="Matilda"/>
</animals>

Thaaaaaaaaanks!

Your buddy,

gooshgoosh

A: 

It seems that you're asking why the leaf-level values in JSON don't translate into attributes in XML, but instead translate into elements?

If that's the real question, then the main answer is consistency. Your approach suddenly switches from holding values in elements to holding them in attributes if they happen to be leaf nodes.

Oh, and holding values in attributes won't work if your value is an array (unless you want to do something bizarre like create attributes "elem0", "elem1", and so on).

Edit: looking at your post again, it seems that one JSON value did turn into an attribute. I would say that you're either misremembering the question, or the person who asked the question didn't give it a lot of thought.

Anon