from flask import Flask, make_response
app = 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 json
from flask import Flask, make_response
app = Flask(__name__)
@app.route("/")
def hello():
data = {"name": "张三"}
res = make_response(json.dumps(data, ensure_ascii=False))
res.mimetype = "application/json"
return res
if __name__ == '__main__':
app.run(debug=True)
这么多代码就等于
import json
from flask import Flask, make_response, jsonify
app = Flask(__name__)
app.config["JSON_AS_ASCII"] = False
@app.route("/")
def hello():
data = {"name": "张三"}
return jsonify(data)
if __name__ == '__main__':
app.run(debug=True)