23 lines
662 B
Python
23 lines
662 B
Python
# 读取json文件
|
|
import json
|
|
|
|
|
|
def read_json(file_path):
|
|
"""
|
|
读取指定JSON文件并以格式化字符串打印内容
|
|
:param file_path: JSON文件路径
|
|
"""
|
|
# 以utf-8的格式打开指定的json文件
|
|
with open(file_path, "r", encoding="utf-8") as f:
|
|
# 使用json.load读取文件内容为python对象
|
|
data = json.load(f)
|
|
# 使用json.dumps将python对象格式化为带有缩进的字符串,确保中文正常显示
|
|
text = json.dumps(data, ensure_ascii=False, indent=2)
|
|
return text
|
|
|
|
|
|
if __name__ == "__main__":
|
|
file_path = "example/example.json"
|
|
result = read_json(file_path)
|
|
print(result)
|