Python字符串转换为字典报错:NameError: name

2020-10-14  本文已影响0人  Monky

问题:Python字符串转换为字典报错:NameError: name ‘null’ is not defined

解决:使用json模块的loads()方法

import json
str = ''' {
      "num": 654321,
      "numId": null,
      "Name": "简书",
      "netId": null,
      "Shorthand": null,
      "bonus": 0
    }
    '''
str_json = json.loads(str)
print(str_json)

打印的结果:

{
  'num': 654321,
  'numId': None,
  'Name': '简书',
  'netId': None,
  'Shorthand': None,
  'bonus': 0
}

转换后,Python自动把 null 转换为了Python支持的None,进行处理。

上一篇 下一篇

猜你喜欢

热点阅读