最新消息:

Flask让jsonify返回中文

flask admin 3272浏览 0评论

Flask 在使用 jsonify 如何返回 中文

VIEW = {
        u"默认": "*",
        u"电信": "DX",
        u"联通": "LT",
        u"移动": "YD",
        u"教育": "JY",
        u"鹏博士": "PBS",
        u"海外": "HW",
        u"局域网": "JYW"
}
# curl 127.0.0.1:8080/api/v1.0/view/list -v
{
  "code": 0,
  "data": {
    "\u5c40\u57df\u7f51": "JYW",
    "\u6559\u80b2": "JY",
    "\u6d77\u5916": "HW",
    "\u7535\u4fe1": "DX",
    "\u79fb\u52a8": "YD",
    "\u8054\u901a": "LT",
    "\u9e4f\u535a\u58eb": "PBS",
    "\u9ed8\u8ba4": "*"
  },
  "error": "",
  "status": "ok"
}

配置 Flask JSON_AS_ASCII 即可

app.config['JSON_AS_ASCII'] = False

json.dumps()解决同样的问题可以加入ensure_ascii=False

http://stackoverflow.com/questions/37531067/python-how-to-prevent-unicode-representation-for-latin1-characters

转载请注明:爱开源 » Flask让jsonify返回中文

您必须 登录 才能发表评论!