【发布时间】:2021-05-08 00:04:32
【问题描述】:
我有一本像这样的字典:
{"first": {"phone": 1900,"other": 1}, "second": {"adwords": 1419, "no_om_source": 1223}}
我将此字典转换为 json 格式。我想将 dict 中的所有数字也更改为字符串。
def convert(o):
if isinstance(o, np.generic): return o.item()
raise TypeError
jsonContent = json.dumps(myDict, default=convert)
with open('data.json', 'w', encoding='utf-8') as f:
json.dump(jsonContent, f, ensure_ascii=False, indent=4)
return jsonContent
但是,当我尝试打印 jsonContent 时,值仍然是数字而不是字符串。我该如何更改?
【问题讨论】:
标签: python json python-3.x numpy dictionary