【问题标题】:return existing JSON file in flask返回烧瓶中现有的 JSON 文件
【发布时间】: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>

非常感谢任何帮助

【问题讨论】:

标签: python json flask


【解决方案1】:

首先解码 request_text

json_result = matching(json.loads(request_text))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-24
    • 1970-01-01
    • 1970-01-01
    • 2021-01-02
    • 2021-02-22
    • 2019-03-23
    • 1970-01-01
    • 2018-09-10
    相关资源
    最近更新 更多