>>> raw_post_data = request.raw_post_data
>>> print raw_post_data
{"group":{"groupId":"2", "groupName":"GroupName"}, "members":{"1":{"firstName":"fName","lastName":"LName","address":"address"},"1": {"firstName":"f_Name","lastName":"L_Name","address":"_address"}}}
>>> create_request = json.loads(raw_post_data)
>>> print create_request
{u'group': {u'groupName': u'GroupName', u'groupId': u'2'}, u'members': {u'1': {u'lastName': u'L_Name', u'firstName': u'f_Name', u'address': u'_address'}}}
如您所见,当我使用json.dumps()
时,键为“1”的成员将被覆盖
有没有办法在python中将其捕获为异常,即在来自客户端的请求中发现重复的密钥?
The rfc 4627 for ^{} media type 建议使用唯一键,但并不明确禁止它们:
来自rfc 2119:
这是answer by jfs的linter固定和类型注释版本。解决了各种绒布突出的问题。Python 3.6+使用f-strings也使其现代化。
或者,如果您想捕获所有重复的键(每个级别),可以使用
collections.Counter
相关问题 更多 >
编程相关推荐