Q:Python字符串转换为字典报错:NameError: name ‘null’ is not defined
A:使用json模块的loads()方法
import json
str = ''' {
"num": 654321,
"numId": null,
"Name": "CSDN博客",
"netId": null,
"Shorthand": null,
"bonus": 0
}
'''
str_json = json.loads(str)
print(str_json)
打印的结果:
{
'num': 654321,
'numId': None,
'Name': 'CSDN博客',
'netId': None,
'Shorthand': None,
'bonus': 0
}
转换后,Python自动把 null 转换为了Python支持的None,进行处理。