Python json.JSONDecodeError 参考
Python json.JSONDecodeError 参考,整理标准库调用语法、参数、示例、返回值和常见错误。
语法
import json
try:
data = json.loads(text)
except json.JSONDecodeError as error:
print(error)
参数
text / file:JSON 字符串或已打开的文件对象。ensure_ascii / indent:控制中文输出和缩进格式。
返回值
返回解析后的 dict/list,或把对象写入字符串/文件;格式错误会抛出 JSONDecodeError。
示例
import json
try:
json.loads("{")
except json.JSONDecodeError as error:
print(error.msg)
常见错误
- JSON 字符串必须使用双引号,不能直接解析 Python 字典写法。
- 写中文时没有设置
ensure_ascii=False,输出会变成 Unicode 转义。
相关内容
相关内容
- Python collections.ChainMap 参考 Python collections.ChainMap 参考,整理标准库调用语法、参数、示例、返回值和常见错误。
- Python collections.Counter 参考 Python collections.Counter 参考,整理标准库调用语法、参数、示例、返回值和常见错误。
- Python collections.defaultdict 参考 Python collections.defaultdict 参考,整理标准库调用语法、参数、示例、返回值和常见错误。
- Python collections.deque 参考 Python collections.deque 参考,整理标准库调用语法、参数、示例、返回值和常见错误。
- Python collections.most_common 参考 Python collections.most_common 参考,整理标准库调用语法、参数、示例、返回值和常见错误。
- Python collections.namedtuple 参考 Python collections.namedtuple 参考,整理标准库调用语法、参数、示例、返回值和常见错误。