{"id":1565,"date":"2018-12-03T19:41:55","date_gmt":"2018-12-03T11:41:55","guid":{"rendered":"http:\/\/ykyi.net\/?p=1565"},"modified":"2018-12-03T19:41:55","modified_gmt":"2018-12-03T11:41:55","slug":"flask%e6%a1%86%e6%9e%b6%e7%9a%84%e5%9b%9e%e5%a4%8d%e4%b8%ad%e6%98%be%e7%a4%ba%e4%b8%ad%e6%96%87utf8%e5%ad%97%e7%ac%a6%ef%bc%8c%e8%80%8c%e4%b8%8d%e6%98%afunicode-escape%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/ykyi.net\/?p=1565","title":{"rendered":"flask\u6846\u67b6\u7684\u56de\u590d\u4e2d\u663e\u793a\u4e2d\u6587utf8\u5b57\u7b26\uff0c\u800c\u4e0d\u662funicode-escape\u5b57\u7b26\u4e32"},"content":{"rendered":"<p>\u4eca\u5929\u5728\u7528flask\u6846\u67b6\u5199http\u670d\u52a1\u7684\u65f6\u5019\uff0c\u53d1\u73b0\u8c03\u7528flask\u7684jsonify\u51fd\u6570\u5f97\u5230\u7684http\u56de\u590d\u5305\u4e2d\u7684\u5b57\u6587\u662f\u7528unicode-escape string\u8868\u793a\u7684\u3002\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"\">u5b57\\u5173\\u6ce8\\u6211\\u4eec\\uff01\n<\/code><\/pre>\n<p>\u6709\u4e9bRESTful\u5de5\u5177\u80fd\u591f\u628aunicode-escape\u5b57\u7b26\u4e32\u8f6c\u6210utf8\uff0c\u80fd\u591f\u6b63\u5e38\u663e\u793a\u51fa\u4e2d\u6587\u3002\u4f46\u5f88\u591a\u7f16\u8f91\u5668\u548c\u5176\u5b83\u5de5\u5177\u53ea\u80fd\u663e\u793a\u539f\u672c\u7684unicode\u8f6c\u4e49\u5b57\u7b26\u4e32\uff0c\u5927\u5927\u59a8\u788d\u8c03\u8bd5\u3002\u89e3\u51b3\u65b9\u6848\u662f\u7528flask\u7684\u53e6\u4e00\u4e2a\u51fd\u6570\uff1amake_response\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python \">from flask import Flask, jsonify, make_response\n\n\ndef mk_utf8resp(js):\n    '''\n    \u4f20\u5165\u4e00\u4e2a\u5b57\u5178\uff0c\u8fd4\u56de\u4e00\u4e2ajson\u683c\u5f0f\u7684http\u56de\u590d\u3002\n    '''\n    resp = make_response(json.dumps(js, ensure_ascii=False))\n    resp.headers['Content-Type'] = 'application\/json'\n    return resp\n<\/code><\/pre>\n<p>\u628ajsonify(js)\u6362\u6210mk_utf8resp(js)\uff0c\u95ee\u9898\u89e3\u51b3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u5728\u7528flask\u6846\u67b6\u5199http\u670d\u52a1\u7684\u65f6\u5019\uff0c\u53d1\u73b0\u8c03\u7528flask\u7684jsonify\u51fd\u6570\u5f97\u5230\u7684http\u56de\u590d\u5305\u4e2d\u7684\u5b57\u6587\u662f\u7528unicode-escape string\u8868\u793a\u7684\u3002\u5982\u4e0b\uff1a u5b57\\u5173\\u6ce8\\u6211\\u4eec\\uff01 \u6709\u4e9bRESTful\u5de5\u5177\u80fd\u591f\u628aunicode-escape\u5b57\u7b26\u4e32\u8f6c\u6210utf8\uff0c\u80fd\u591f\u6b63\u5e38\u663e\u793a\u51fa\u4e2d\u6587\u3002\u4f46\u5f88\u591a\u7f16\u8f91\u5668\u548c\u5176\u5b83\u5de5\u5177\u53ea\u80fd\u663e\u793a\u539f\u672c\u7684unicode\u8f6c\u4e49\u5b57\u7b26\u4e32\uff0c\u5927\u5927\u59a8\u788d\u8c03\u8bd5\u3002\u89e3\u51b3\u65b9\u6848\u662f\u7528flask\u7684\u53e6\u4e00\u4e2a\u51fd\u6570\uff1amake_response\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002\u4ee3\u7801\u5982\u4e0b\uff1a from flask import Flask, jsonify, make_response def mk_utf8resp(js): &#8221;&#8217; \u4f20\u5165\u4e00\u4e2a\u5b57\u5178\uff0c\u8fd4\u56de\u4e00\u4e2ajson\u683c\u5f0f\u7684http\u56de\u590d\u3002 &#8221;&#8217; resp = make_response(json.dumps(js, ensure_ascii=False)) resp.headers[&#8216;Content-Type&#8217;] = &#8216;application\/json&#8217; return resp \u628ajsonify(js)\u6362\u6210mk_utf8resp(js)\uff0c\u95ee\u9898\u89e3\u51b3\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,9],"tags":[32],"class_list":["post-1565","post","type-post","status-publish","format-standard","hentry","category-tech_articles","category-web-2","tag-flask"],"_links":{"self":[{"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/posts\/1565","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ykyi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1565"}],"version-history":[{"count":0,"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/posts\/1565\/revisions"}],"wp:attachment":[{"href":"https:\/\/ykyi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ykyi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ykyi.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}