read_json.py 1015 Bytes
Newer Older
cxk committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   test62
# FileName:      read_json.py
# Author:       lao_zhao
# Datetime:     2024/9/4 14:31
# Description:
# 
# ---------------------------------------------------------------------------
import json
from apiAutoTest_v3 import log


def read_json(file_path):
    """读取json文件,将json文件的数据序列化为python对象再返回"""
    if isinstance(file_path, str):
        try:
            with open(file_path, mode="r", encoding="utf-8") as f:
                return json.load(f)
        except Exception as e:
            log.error(f"读取json文件失败,传入的文件路径:{file_path}-可能文件路径不存在,也有可能文件的数据格式有错误,错误为:{e}")
            raise e
    else:
        log.error(f"执行read_json失败,传入的文件路径:{file_path}-不为字符串")
        raise ValueError("传入的文件路径必须为字符串")