Does JSON syntax allow duplicate keys in an object?

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

    The standard does say this:

    Programming languages vary widely on whether they support objects, and if so, what characteristics and constraints the objects offer. The models of object systems can be wildly divergent and are continuing to evolve. JSON instead provides a simple notation for expressing collections of name/value pairs. Most programming languages will have some feature for representing such collections, which can go by names like record, struct, dict, map, hash, or object.

    The bug is in node.js at least. This code succeeds in node.js.

    try {
         var json = {"name":"n","name":"v"};
         console.log(json); // outputs { name: 'v' }
    } catch (e) {
         console.log(e);
    }
    

提交回复
热议问题