擅长:python、mysql、java
<p><a href="http://tools.ietf.org/html/rfc4627#section-2.2">The rfc 4627 for ^{<cd1>} media type</a>建议使用唯一键,但并不明确禁止它们:</p>
<blockquote>
<p>The names within an object SHOULD be unique.</p>
</blockquote>
<p>来自<a href="http://tools.ietf.org/html/rfc2119">rfc 2119</a>:</p>
<blockquote>
<p><strong>SHOULD</strong> This word, or the adjective "RECOMMENDED", mean that there<br/>
may exist valid reasons in particular circumstances to ignore a<br/>
particular item, but the full implications must be understood and<br/>
carefully weighed before choosing a different course.</p>
</blockquote>
<pre><code>import json
def dict_raise_on_duplicates(ordered_pairs):
"""Reject duplicate keys."""
d = {}
for k, v in ordered_pairs:
if k in d:
raise ValueError("duplicate key: %r" % (k,))
else:
d[k] = v
return d
json.loads(raw_post_data, object_pairs_hook=dict_raise_on_duplicates)
# -> ValueError: duplicate key: u'1'
</code></pre>