python - 我的烧瓶应用程序没有正确返回非 ascii 字符
问题描述
我正在尝试使用flask-restful api,作为返回值,代码应该返回一个json数据列表。但是,当 json 中的内容是非 ascii 字符(如 (èòsèèò))时,返回值
这是一个示例代码:
#! /usr/bin/env python
# coding: utf-8
from flask import Flask, Response
from flask_restful import Resource, Api
import json
app = Flask(__name__)
# Create the API
API = Api(app)
@app.route('/')
def hello_world():
return Response('Here, with Response it works well: höne')
class APICLASS(Resource):
"""
"""
def get(self, id):
return [
{
"hey": 11,
"test": "höne"
}], 200
API.add_resource(APICLASS, '/<string:id>')
if __name__ == '__main__':
app.run(debug=True)
但是当我在 localhost 上检查结果时,我看到以下输出:
[
{
"hey": 11,
"test": "h\u00f6ne"
}]
解决方案
显然,它与此错误有关。我不确定是否有任何副作用,但这可能会有所帮助:
# ...
app = Flask(__name__)
# Create the API
API = Api(app)
API.app.config['RESTFUL_JSON'] = {
'ensure_ascii': False
}
@app.route('/')
# Rest of your code
推荐阅读
- php - 使用 mpdf 生成 pdf 如何在 PHP 中生成多个 pdf
- c# - 如何告诉 Entity Framework Core 5 两个实体不相关?
- javascript - 为什么快递服务器侦听的端口与我的反应应用程序所在的端口不同
- excel - VBA:ComboBox 上的数据验证
- javascript - 在函数参数中传递对象
- opencv - 如何准确检测和定位汽车保险丝?
- android - 从数据类连接 Kotlin 中的字符串数组
- html - 我如何为第 n 个孩子使用 float left
- typescript - TypeScript 中方差、协方差、逆变和双方差的区别
- css - 如何从当前位置(不是来自父级)定位(偏移)绝对元素?