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))

输出结果正常:
<class 'str'>
[{'name': 'zhang san', 'gender': 'male', 'birthday': '1992-01-05'}, {'name': 'li si', 'gender': 'female', 'birthday': '1991-12-13'}]
<class 'list'>

注意:将标识字符的单引号替换为双引号时,需要先将外面的三双引号替换为三单引号

GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e 2 个月前
8c391e04 5 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐