如何将页面响应为 json 页面?下面的代码是否正确?
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import json
class JsonPage(webapp.RequestHandler):
def get(self):
self.response.header['Content-Type'] = 'application/json'
self.response.out.write(json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}]))
application = webapp.WSGIApplication([('json', JsonPage)], debug=True)
def main():
run_wsgi_app(application)
if __name__ == '__main__':
main()
我收到以下错误消息:
HTTP request sent, awaiting response... 404 Not Found
2012-05-06 14:10:01 ERROR 404: Not Found.
对代码进行一些更改后,出现以下错误,似乎在 json 模块中找不到转储对象:
<pre>Traceback (most recent call last):
File "/opt/google_appengine/google/appengine/ext/webapp/_webapp25.py", line 701, in __call__
handler.get(*groups)
File "/home/kelvin/workspace/cloudnuts/json.py", line 8, in get
self.response.out.write(json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}]))
AttributeError: 'module' object has no attribute 'dumps'
</pre>