根据this conversion table,Python int在使用JSON模块进行序列化时会被写成JSON数字——正如我所期望和期望的那样.
我有一个具有整数键和整数值的字典:
>>> d = {1:2}
>>> type(d.items()[0][0])
<type 'int'>
>>> type(d.items()[0][1])
<type 'int'>
When I use the json module to serialize this to a JSON string, the value is written as a number, but the key is written as a string:
>>> json.dumps(d)
'{"1": 2}'
This isn't the behavior I want, and it seems particularly broken since it breaks json.dumps/json.loads round-tripping:
>>> d == json.loads(json.dumps(d))
False
为什么会发生这种情况,有没有办法强制将密钥写成一个数字?