具体步骤1、保存json文件格式示例如下[{id:S001,model:A,passed:true,latency_ms:1999},{id:S002,model:A,passed:true,latency_ms:2000},{id:S003,model:A,passed:true,latency_ms:2001},{id:S004,model:A,passed:false,latency_ms:1999},{id:S005,model:A,passed:false,latency_ms:2000},{id:S006,model:A,passed:false,latency_ms:2001}]2、读取文件的最小语法importjsonfrompathlibimportPath##获取路径__file__指当前脚本resolve()通过解析所有符号链接使路径变为绝对路径parent取所在目录with ... as ...代码块结束后自动关闭文件BASE_DIRPath(__file__).resolve().parent input_pathBASE_DIR/tests.json##读取函数open().read()encodingutf-8指定使用 UTF-8 字符编码防止写入中文或特殊字符时出现乱码withinput_path.open(r,encodingutf-8)asf:test_casesjson.load(f)print(type(test_cases),len(test_cases))3、保存文件的最小语法output_pathBASE_DIR/results.json## 写入函数open().write()json.dump(...)把 Python 对象这里是 report通常是一个字典 dict 或列表 list转换成 JSON 格式并直接写入到文件对象 f 中ensure_asciiFalse让中文直接显示indent2设置缩进为 2 个空格withoutput_path.open(w,encodingutf-8)asf:json.dump(report,f,ensure_asciiFalse,indent2)