在Python中读取JSON文件可以使用内置的json模块。下面是一个完整的示例代码:
import json
# 读取JSON文件
def read_json_file(file_path):
with open(file_path, 'r') as file:
data = json.load(file)
return data
# JSON文件路径
json_file_path = 'path/to/your/json/file.json'
# 调用函数读取JSON文件
json_data = read_json_file(json_file_path)
# 打印JSON数据
print(json_data)
上述代码中,首先导入了json模块。然后定义了一个read_json_file
函数,该函数接受一个文件路径作为参数,使用open
函数打开文件,并使用json.load
方法将文件内容解析为JSON数据。最后,调用read_json_file
函数并传入JSON文件的路径,将返回的JSON数据存储在json_data
变量中,并打印出来。
请注意,你需要将'path/to/your/json/file.json'
替换为你实际的JSON文件路径。
领取专属 10元无门槛券
手把手带您无忧上云