Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Flask无法从发送文件响应对象获取JSON_Python_Flask - Fatal编程技术网

Python Flask无法从发送文件响应对象获取JSON

Python Flask无法从发送文件响应对象获取JSON,python,flask,Python,Flask,使用Flask,我尝试使用send\u from\u directory加载静态JSON文件。从中我得到一个响应对象。我无法以JSON格式获取此对象;不过 courses = send_from_directory("static/", "courses.json"); courses["math"] # not a JSON - cannot get math element from loaded courses jsonify(courses) # Object of type Respo

使用Flask,我尝试使用
send\u from\u directory
加载静态JSON文件。从中我得到一个
响应
对象。我无法以JSON格式获取此对象;不过

courses = send_from_directory("static/", "courses.json");
courses["math"] # not a JSON - cannot get math element from loaded courses
jsonify(courses) # Object of type Response is not JSON serializable
那么,如何将响应转换为JSON格式呢

编辑:澄清一下-我不想返回文件和JSON。我试图从位于工作目录“/static”中的JSON文件中获取一个
dict

尝试以下操作:

import json

courses = send_from_directory("static/", "courses.json");

courses_dict = json.loads(courses)
print(courses_dict["math"])