from flask import Flask, make_responseapp = Flask(__name__)@app.route("/")
def hello():data = {"name": "张三"}return make_response(data)if __name__ == '__main__':app.run(debug=True)
如果像返回字符串那么可以用
return make_response(json.dumps(data, ensure_ascii=False))
import jsonfrom flask import Flask, make_responseapp = Flask(__name__)@app.route("/")
def hello():data = {"name": "张三"}res = make_response(json.dumps(data, ensure_ascii=False))res.mimetype = "application/json"return resif __name__ == '__main__':app.run(debug=True)
这么多代码就等于
import jsonfrom flask import Flask, make_response, jsonifyapp = Flask(__name__)app.config["JSON_AS_ASCII"] = False@app.route("/")
def hello():data = {"name": "张三"}return jsonify(data)if __name__ == '__main__':app.run(debug=True)