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

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

问题描述

这个有效的json吗?

Is this valid json?

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

http://jsonlint.com/表示同意.

http://www.json.org/并没有说什么被禁止.

http://www.json.org/ doesn't say anything about it being forbidden.

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

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

推荐答案

来自标准(第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库实现 not 不接受重复的键与标准不冲突.

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天全站免登陆