在python中使用clone或tornado,如何创建一个简单的处理程序来响应json?

2024-04-29 02:31:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我是这样做的吗?在

import cyclone

class MyHandler(cyclone.web.RequestHandler):
    def get(self, command):
        details = {'status':'success'}
        json = json_encode(details)
        self.write(json)

或者还有更多吗?在


Tags: importselfwebjsongetdefstatusdetails
2条回答

我建议使用内置的json encoder function

self.write(tornado.escape.json_encode(details)

如果details是dict类型,tornado将自动对数据进行json编码。对于列表来说,情况并非如此。 从龙卷风密码(网页.py)公司名称:

Note that lists are not converted to JSON because of a potential cross-site security vulnerability. All JSON output should be wrapped in a dictionary. More details at http://haacked.com/archive/2008/11/20/anatomy-of-a-subtle-json-vulnerability.aspx

它甚至比这还少:您可以简单地使用self.write(details)如果您编写dict,它将自动转换为JSON。在

相关问题 更多 >