返回flask中的现有JSON文件

2024-04-19 04:45:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个返回JSON文件的方法(matching)。我想通过flask方法返回此文件

当我这样做时:

@app.route('/search', methods=['POST'])
def json_example():
    req_data = request.get_json()
    request_text = req_data['request'] #json text that is needed as argument for calling the matching method
    json_result = matching(request_text)
    return json_result

我得到这个错误:

<title>TypeError: The view function did not return a valid response. The return type must be a string, dict, tuple,
    Response instance, or WSGI callable, but it was a TextIOWrapper. // Werkzeug Debugger</title>

在玩str(json_result)时,我得到了以下信息:

<_io.TextIOWrapper name='result.json' mode='w' encoding='UTF-8'>

另外,jsonify(json_reslult)不起作用,因为我有一个json文件,并且没有尝试将字符串转换为json。在尝试时,它提供:

<title>TypeError: Object of type TextIOWrapper is not JSON serializable // Werkzeug Debugger</title>

非常感谢您的帮助


Tags: 文件方法textjsondatareturntitleis