any idea how to output a JSON object in python using Tornado. Any good examples, tutorial,libraries or one line code which outputs a JSONP object.
Tornado provides tornado.escape.json_encode
, which simply wraps json
on Python 2.6+ or simplejson
on Python 2.5. It's simple to use:
from tornado.escape import json_encode
obj = {
'foo': 'bar',
'1': 2,
'false': True
}
self.write(json_encode(obj))
outputs:
{"1": 2, "foo": "bar", "false": true}
For a JSONP response:
callback = self.get_argument('callback')
jsonp = "{jsfunc}({json});".format(jsfunc=callback,
json=json_encode(obj))
self.set_header('Content-Type', 'application/javascript')
self.write(jsonp)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With