How would I express XML tag attributes in JSON?

后端 未结 13 2212
遥遥无期
遥遥无期 2020-12-07 20:35

I am designing an API for my webapp.

I was thinking to support only JSON responses (not XML) because more streamlined.

But I have just bumped to this XML:

13条回答
  •  旧时难觅i
    2020-12-07 21:29

    It seems to me that the most exact correspondence between XML and JSON would need to represent an XML node as a triplet (i.e. array): [name, attributes, value], with name being a string, attributes an object with attribute names as keys and attribute values as (string) values, and value a string (for atomic values) or an array of such triplets.

    By such mapping the JSON-equivalent of

    
        Shopping
    
    

    would be

    [  "folders",
       {}, 
       [
          [  "folder", 
             {   "id": "123",
                 "private": "0",
                 "archived": "0",
                 "order": "1"
             },
             "Shopping"
          ]
       ]
    ]
    

    Actually the idea behind this mapping is that:

    1) XML-JSON transformation be reversible. 2) The "sibling" relationship of sub-nodes be preserved

    At the same time the distinction between attribute nodes and value nodes is explicit here.

    Does it make sense? And does it justify the complexity overhead?

提交回复
热议问题