import json
str = """
[{
'name':'zhang san',
'gender':'male',
'birthday':'1992-01-05'
},{
'name':'li si',
'gender':'female',
'birthday':'1991-12-13'
}]
"""
print(type(str))
data = json.loads(str)
print(data)
print(type(data))
运行报错:
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 3 column 5 (char 8)
原因分析:
str字符串里面的标识字符用了单引号括起来,将其换成双引号后再次运行
import json
str = '''
[{
"name":"zhang san",
"gender":"male",
"birthday":"1992-01-05"
},{
"name":"li si",
"gender":"female",
"birthday":"1991-12-13"
}]
'''
print(type(str))
data = json.loads(str)
print(data)
print(type(data))
输出结果正常:
[{'name': 'zhang san', 'gender': 'male', 'birthday': '1992-01-05'}, {'name': 'li si', 'gender': 'female', 'birthday': '1991-12-13'}]
注意:将标识字符的单引号替换为双引号时,需要先将外面的三双引号替换为三单引号