【发布时间】:2020-09-23 10:49:42
【问题描述】:
我有一个返回 JSON 文件的方法 (matching)。我想通过烧瓶方法返回这个文件。
当我这样做时:
@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>
非常感谢任何帮助
【问题讨论】:
-
@Marco 不起作用,因为我已经有一个 json,我不需要将我的字符串转换为 json 但返回我的 json
-
也没有工作,很遗憾
-
试试这个:return json_result.read()