Does JSON syntax allow duplicate keys in an object?

后端 未结 12 2141
青春惊慌失措
青春惊慌失措 2020-11-22 00:05

Is this valid json?

{
    \"a\" : \"x\",
    \"a\" : \"y\"
}

http://jsonlint.com/ says yes.

http://www.json.org/ doesn\'t say anyth

12条回答
  •  Happy的楠姐
    2020-11-22 00:06

    SHOULD be unique does not mean MUST be unique. However, as stated, some parsers would fail and others would just use the last value parsed. However, if the spec was cleaned up a little to allow for duplicates then I could see a use where you may have an event handler which is transforming the JSON to HTML or some other format... In such cases it would be perfectly valid to parse the JSON and create another document format...

    [
      "div":
      {
        "p": "hello",
        "p": "universe"
      },
      "div":
      {
        "h1": "Heading 1",
        "p": "another paragraph"
      }
    ]
    

    could then easily parse to html for example:

    
     

    hello

    universe

    Heading 1

    another paragraph

    I can see the reasoning behind the question but as it stands... I wouldn't trust it.

提交回复
热议问题