【问题标题】:Flask cannot get JSON from send_file Response objectFlask 无法从 send_file 响应对象中获取 JSON
【发布时间】:2020-03-04 19:53:42
【问题描述】:

使用 Flask,我尝试使用 send_from_directory 加载静态 JSON 文件。从这里我得到一个Response 对象。我无法以 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

【问题讨论】:

    标签: python flask


    【解决方案1】:

    试试这个:

    import json
    
    courses = send_from_directory("static/", "courses.json");
    
    courses_dict = json.loads(courses)
    print(courses_dict["math"])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-07
      • 2018-08-31
      • 2020-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-12
      相关资源
      最近更新 更多