Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
722 views
in Technique[技术] by (71.8m points)

json - python jsonify dictionary in utf-8

I want to get json data into utf-8

I have a list my_list = []

and then many appends unicode values to the list like this

my_list.append(u'????')

return jsonify(result=my_list)

and it gets

{
"result": [
"u10e2u10d4u10e1u10e2",
"u10e2u10ddu10dbu10d0u10e8u10d5u10d8u10dau10d8"
]
}
question from:https://stackoverflow.com/questions/14853694/python-jsonify-dictionary-in-utf-8

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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():

>>> data = u'u10e2u10d4u10e1u10e2'
>>> import json
>>> json.dumps(data)
'"\u10e2\u10d4\u10e1\u10e2"'
>>> json.dumps(data, ensure_ascii=False)
u'"u10e2u10d4u10e1u10e2"'
>>> print json.dumps(data, ensure_ascii=False)
"????"
>>> json.dumps(data, ensure_ascii=False).encode('utf8')
'"xe1x83xa2xe1x83x94xe1x83xa1xe1x83xa2"'

Note that you still need to explicitly encode the result to UTF8 because the dumps() function returns a unicode object in that case.

You can make this the default (and use jsonify() again) by setting JSON_AS_ASCII to False in your Flask app config.

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. 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 u2028 and u2029 sequences.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...