Tornado提供了
tornado.escape.json_enpre,可将其简单地包装
json在Python
2.6+或
simplejsonPython 2.5上。使用简单:
from tornado.escape import json_enpreobj = { 'foo': 'bar', '1': 2, 'false': True }self.write(json_enpre(obj))输出:
{"1": 2, "foo": "bar", "false": true}对于JSONP响应:
callback = self.get_argument('callback')jsonp = "{jsfunc}({json});".format(jsfunc=callback, json=json_enpre(obj))self.set_header('Content-Type', 'application/javascript')self.write(jsonp)


