Reference Python 标准库 reference

Python json.load 参考

Python json.load 参考,整理标准库调用语法、参数、示例、返回值和常见错误。

语法

python
import json

with open("data.json", "r", encoding="utf-8") as file:
    data = json.load(file)

参数

  • text / file:JSON 字符串或已打开的文件对象。
  • ensure_ascii / indent:控制中文输出和缩进格式。

返回值

返回解析后的 dict/list,或把对象写入字符串/文件;格式错误会抛出 JSONDecodeError。

示例

python
import json

with open("data.json", "r", encoding="utf-8") as file:
    data = json.load(file)
print(data)

常见错误

  • JSON 字符串必须使用双引号,不能直接解析 Python 字典写法。
  • 写中文时没有设置 ensure_ascii=False,输出会变成 Unicode 转义。

相关内容