JSON 语法是否允许对象中的重复键? [英] Does JSON syntax allow duplicate keys in an object?

查看:50
本文介绍了JSON 语法是否允许对象中的重复键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是有效的 json 吗?

Is this valid json?

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

http://jsonlint.com/ 说是.

http://www.json.org/ 没有说明它被禁止.

但显然它没有多大意义,不是吗?大多数实现可能使用哈希表,因此无论如何它都会被覆盖.

But obviously it doesn't make much sense, does it? Most implementations probably use a hashtable so it is being overriden anyways.

推荐答案

来自 标准 (p. ii):

预计其他标准会参考这个,严格遵循JSON文本格式,而对各种编码细节施加限制.此类标准可能需要特定的行为.JSON本身不指定任何行为.

It is expected that other standards will refer to this one, strictly adhering to the JSON text format, while imposing restrictions on various encoding details. Such standards may require specific behaviours. JSON itself specifies no behaviour.

在标准 (p. 2) 的进一步下方,JSON 对象的规范:

Further down in the standard (p. 2), the specification for a JSON object:

一个对象结构被表示为一对围绕零个或多个名称/值对的大括号标记.名称是一个字符串.每个名称后面都有一个冒号标记,将名称与值分开.一个逗号标记将值与以下名称分开.

An object structure is represented as a pair of curly bracket tokens surrounding zero or more name/value pairs. A name is a string. A single colon token follows each name, separating the name from the value. A single comma token separates a value from a following name.

它没有提到重复的密钥无效或有效,所以根据规范,我可以放心地假设这意味着它们是允许的.

It does not make any mention of duplicate keys being invalid or valid, so according to the specification I would safely assume that means they are allowed.

JSON 库的大多数实现接受重复键与标准不冲突,因为第一个引号.

That most implementations of JSON libraries do not accept duplicate keys does not conflict with the standard, because of the first quote.

这里有两个与 C++ 标准库相关的示例.当将一些 JSON 对象反序列化为 std::map 时,拒绝重复键是有意义的.但是当将一些 JSON 对象反序列化为 std::multimap 时,正常接受重复键是有意义的.

Here are two examples related to the C++ standard library. When deserializing some JSON object into a std::map it would make sense to refuse duplicate keys. But when deserializing some JSON object into a std::multimap it would make sense to accept duplicate keys as normal.

这篇关于JSON 语法是否允许对象中的重复键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆