# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   test_58
# FileName:      read_json.py
# Author:       xxxxxxx
# Datetime:     2024/1/4 9:48
# Description:
# 
# ---------------------------------------------------------------------------
import json
import os


def read_json(filename: str):
    """根据文件的路径，读取json文件，并将json文件的数据转成python对象并返回"""
    # 判断传入的文件路径是否合法并且是.json结尾
    if os.path.isfile(filename) and filename.endswith(".json"):
        # 以读的模式打开文件
        with open(filename, mode="r", encoding="utf-8") as f:
            try:
                # 读取文件中的内容，并将内容转成python对象
                res_obj = json.loads(f.read())
            except:
                raise FileExistsError("将文件的内容转成python对象出错~~")
            else:
                return res_obj
    else:
        raise FileNotFoundError("传入的json文件错误")
