Does JSON syntax allow duplicate keys in an object?

后端 未结 12 2132
青春惊慌失措
青春惊慌失措 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:25

    It's not defined in the ECMA JSON standard. And generally speaking, a lack of definition in a standard means, "Don't count on this working the same way everywhere."

    If you're a gambler, "many" JSON engines will allow duplication and simply use the last-specified value. This:

    var o = {"a": 1, "b": 2, "a": 3}
    

    Becomes this:

    Object {a: 3, b: 2}
    

    But if you're not a gambler, don't count on it!

提交回复
热议问题