您的位置:宽带测速网 > 编程知识 > python如何读取json并解析

python如何读取json并解析

2025-06-23 19:17来源:互联网 [ ]

要读取和解析JSON文件,可以使用Python的内置模块json。以下是一个简单的示例:

import json# 读取JSON文件with open('data.json', 'r') as file:data = json.load(file)# 解析JSON数据for key, value in data.items():print(key, value)

在上面的示例中,我们首先使用open函数打开一个JSON文件,然后使用json.load方法加载文件中的数据。接着,我们遍历数据并打印键值对。