Does JSON syntax allow duplicate keys in an object?

后端 未结 12 1910
青春惊慌失措
青春惊慌失措 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条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 00:21

    There are 2 documents specifying the JSON format:

    1. http://json.org/
    2. https://tools.ietf.org/html/rfc7159

    The accepted answer quotes from the 1st document. I think the 1st document is more clear, but the 2nd contains more detail.

    The 2nd document says:

    1. Objects

      An object structure is represented as a pair of curly brackets surrounding zero or more name/value pairs (or members). A name is a string. A single colon comes after each name, separating the name from the value. A single comma separates a value from a following name. The names within an object SHOULD be unique.

    So it is not forbidden to have a duplicate name, but it is discouraged.

提交回复
热议问题