import json
a = '{"t": 1, "c": "a"}'
a = json.loads(a)
print(a["t"])
这样对字符串转 dict 是正常的。
但是如果
import json
a = "{'t': 1, 'c': 'a'}"
a = json.loads(a)
会报json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
import json
a = "{'t': 1, 'c': 'a'}"
a = json.dumps(a)
a = json.loads(a)
print(a["t"])
在进行loads()的时候没有报错,但是在调用的时候报TypeError: string indices must be integers
看了一圈资料有点懵,JSON 是不允许用单引号括起 key 的,Python 的 JSON 模块可以dumps转一下再loads但是就有点没理解了