简体   繁体   English

utf-8中的python jsonify字典

[英]python jsonify dictionary in utf-8

I want to get json data into utf-8我想将 json 数据转换为 utf-8

I have a list my_list = []我有一个列表my_list = []

and then many appends unicode values to the list like this然后许多人像这样将 unicode 值附加到列表中

my_list.append(u'ტესტ')

return jsonify(result=my_list)

and it gets它得到

{
"result": [
"\u10e2\u10d4\u10e1\u10e2",
"\u10e2\u10dd\u10db\u10d0\u10e8\u10d5\u10d8\u10da\u10d8"
]
}

使用以下配置添加 UTF-8 支持:

app.config['JSON_AS_ASCII'] = False

Use the standard-library json module instead, and set the ensure_ascii keyword parameter to False when encoding, or do the same with flask.json.dumps() :改用标准库json模块,并在编码时将ensure_ascii关键字参数设置为 False,或对flask.json.dumps()执行相同flask.json.dumps()

>>> data = u'\u10e2\u10d4\u10e1\u10e2'
>>> import json
>>> json.dumps(data)
'"\\u10e2\\u10d4\\u10e1\\u10e2"'
>>> json.dumps(data, ensure_ascii=False)
u'"\u10e2\u10d4\u10e1\u10e2"'
>>> print json.dumps(data, ensure_ascii=False)
"ტესტ"
>>> json.dumps(data, ensure_ascii=False).encode('utf8')
'"\xe1\x83\xa2\xe1\x83\x94\xe1\x83\xa1\xe1\x83\xa2"'

Note that you still need to explicitly encode the result to UTF8 because the dumps() function returns a unicode object in that case.请注意,您仍然需要将结果显式编码为 UTF8,因为在这种情况下dumps()函数返回一个unicode对象。

You can make this the default (and use jsonify() again) by setting JSON_AS_ASCII to False in your Flask app config.您可以通过在 Flask 应用程序配置JSON_AS_ASCII设置为 False使其成为默认值(并再次使用jsonify() )。

WARNING : do not include untrusted data in JSON that is not ASCII-safe, and then interpolate into a HTML template or use in a JSONP API, as you can cause syntax errors or open a cross-site scripting vulnerability this way.警告:不要在非 ASCII 安全的 JSON 中包含不受信任的数据,然后插入 HTML 模板或在 JSONP API 中使用,因为这样可能导致语法错误或打开跨站点脚本漏洞。 That's because JSON is not a strict subset of Javascript , and when disabling ASCII-safe encoding the U+2028 and U+2029 separators will not be escaped to \
 and \
 sequences.这是因为JSON 不是 Javascript 的\
,并且在禁用 ASCII 安全编码时, U+2028 和 U+2029 分隔符不会转义为\
\
序列。

If you still want to user flask's json and ensure the utf-8 encoding then you can do something like this:如果您仍然想使用flask 的json 并确保使用utf-8 编码,那么您可以执行以下操作:

from flask import json,Response
@app.route("/")
def hello():
    my_list = []
    my_list.append(u'ტესტ')
    data = { "result" : my_list}
    json_string = json.dumps(data,ensure_ascii = False)
    #creating a Response object to set the content type and the encoding
    response = Response(json_string,content_type="application/json; charset=utf-8" )
    return response

#I hope this helps #我希望这有帮助

In my case the above solution was not sufficient.就我而言,上述解决方案是不够的。 (Running flask on the GCP App Engine flexible environment). (在 GCP App Engine 柔性环境上运行 Flask)。 I ended up doing:我最终做了:

json_str = json.dumps(myDict, ensure_ascii = False, indent=4, sort_keys=True)
encoding = chardet.detect(json_str)['encoding']
json_unicode = json_str.decode(encoding)
json_utf8 = json_unicode.encode('utf-8')
response = make_response(json_utf8)
response.headers['Content-Type'] = 'application/json; charset=utf-8'
response.headers['mimetype'] = 'application/json'
response.status_code = status

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM