将 Python dicts 中的整数键转换为符合 JSON 标准的字符串键json.dumps()
是有损的:一旦完成,就无法判断原始键是整数23
还是字符串'23'
(除非该信息存储在其他地方)。
也就是说,您可以json.loads()
通过将适当的函数作为参数传递来强制将键转换为整数object_pairs_hook
:
def int_keys(ordered_pairs):
result = {}
for key, value in ordered_pairs:
try:
key = int(key)
except ValueError:
pass
result[key] = value
return result
用法:
>>> import json
>>> data = {1: '1', 2: '2', 3: '3'}
>>> text = json.dumps(data)
>>> text
'{"1": "1", "2": "2", "3": "3"}'
>>> json.loads(text, object_pairs_hook=int_keys)
{1: '1', 2: '2', 3: '3'}
对此进行扩展,还可以编写一个不仅可以转换整数,还可以转换所有其他可能已转换为字符串object_pairs_hook
的非字符串键:json.dumps()
SPECIAL = {
"true": True,
"false": False,
"null": None,
}
def round_trip(ordered_pairs):
result = {}
for key, value in ordered_pairs:
if key in SPECIAL:
key = SPECIAL[key]
else:
for numeric in int, float:
try:
key = numeric(key)
except ValueError:
continue
else:
break
result[key] = value
用法:
>>> print(more_text)
{
"2": 2,
"3.45": 3.45,
"true": true,
"false": false,
"null": null,
"Infinity": Infinity,
"-Infinity": -Infinity,
"NaN": NaN
}
>>> json.loads(more_text, object_pairs_hook=round_trip)
{2: 2, 3.45: 3.45, True: True, False: False, None: None, inf: inf, -inf: -inf, nan: nan}