搜尋

首頁  >  問答  >  主體

python - flask-restful 中文回傳的回應變成了 unicode literal

下面的範例程式碼,運行在 Python 2.7 環境

pip install flask-restful flask
# -*- coding:utf-8 -*-
from flask import Flask
from flask_restful import Api, Resource, reqparse, marshal_with, fields

class Greetings(Resource):
    def get(self, message=None):
        message = '中文한국어'
        greeting = 'You have received a message: {0}'.format(message) if message else 'zzzzzzz......'
        return greeting, 200

app = Flask(__name__)

api = Api(app)

api.add_resource(Greetings, '/radio/', '/radio/<message>')


if __name__ == '__main__':
    app.run(port=8000,debug=True)

這是運行的結果:

我想大声告诉你我想大声告诉你2747 天前832

全部回覆(1)我來回復

  • 为情所困

    为情所困2017-05-18 10:59:32

    指定 RESTFUL_JSON 配置項:

    app = Flask(__name__)
    app.config.update(RESTFUL_JSON=dict(ensure_ascii=False))

    回覆
    0
  • 取消回覆