How would I express XML tag attributes in JSON?

后端 未结 13 2178
遥遥无期
遥遥无期 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条回答
  •  时光取名叫无心
    2020-12-07 21:34

    edit

    A similar method is advocated on http://www.jsonml.org/. They coined the term json markup language.


    You can pick any mapping you like, but if you map

    
      txt
    
    

    to

    {"el":{"attr":"value","content":"txt"}}
    

    then how would you map:

    txt2
    

    I'd make use of the fact that some attibute names are forbidden.

    {"el":{"attr":"value", "content":"txt1", "":["txt"]}
    

    Or a more compex example:

    
      on
      
        I just put some text here
        main_window
        500
        500
      
      
        250mm
        250
        center
      
    
    

    could map to:

    {"widget":{"":[
      {"debug":{"":["on"]}},
      {"window":{"title":"Sample Konfabulator Widget", "": [
        "I just put some text here",
        {"name":{"":["main window"]}},
        {"width":{"":["500"]}},
        {"height":{"":["500"]}}
      ]},
      {"image":{"src":"Images/Sun.png", "name":"sun1", "":[
        {"hOffset":{"":["250",{"unit":{"":["mm"]}}]}},
        {"vOffset":{"":["250"]}},
        {"alignment":{"":["center"]}}
      }
    ]}
    

    The rules for this conversion are unambiguous:

    • an element is converted to an object.
    • The object key is the element name.
    • The object value is an object itself, with:
      • an attibute is mapped to an object property. (key/value)
      • the object content is mapped to a special property.
        • The special property name is an empty string. This can never collide with an xml attribyte name.
        • The special property value is an array.
        • Within the array, plain text is represented as a string.
        • Within the array, elements are represented as objects, as described above.

    To safe space, there is a way to unambigously simplify mentioned mapping:

    {"widget":{"":[
      {"debug":"on"},
      {"window":{"title":"Sample Konfabulator Widget", "": [
        "I just put some text here",
        {"name":"main window"},
        {"width":"500"},
        {"height":"500"}
      ]},
      {"image":{"src":"Images/Sun.png", "name":"sun1", "":[
        {"hOffset":["250",{"unit":"mm"}]},
        {"vOffset":"250"},
        {"alignment":"center"}
      }
    ]}
    
    • If an element doesn't have any attibutes, the value object (containing the special empty string mapping to an array) is replaced by the array directly. So instead of:

      {"hOffset":{"":["250",{"unit":{"":["mm"]}}]}}

    you get

    {"hOffset":["250",{"unit":["mm"]}]}
    
    • If the element content is just text, the array containing the string value is replaced by the string value directly, so you get:

      {"hOffset":["250",{"unit":"mm"}]}

    This way, there will always be exactly one way to map the jml (json markup language) back to xml (or html)

提交回复
热议问题