7

最近一直在开发 appengine 应用程序。我想解析对应用程序的请求中包含的 json 数据。如何使用 requesthandler 类的 request 对象来实现这一点?

下面是一段代码,显示我想要实现的目标:

import cgi
import webapp2
import datamethods

from google.appengine.ext.webapp.util import run_wsgi_app

class adduser(webapp2.RequestHandler):
    def get(self):
        # Get the phone number from json data in request.
        userphone = self.request.get("phone")
        # Get the name from json data in request.
        name = self.request.get("name")


app = webapp2.WSGIApplication([
  ('/adduser', adduser),
  ('/sign', updatestatus),
  ('/login',login)
], debug=True)


def main():
    run_wsgi_app(app)

if __name__ == "__main__":
    main() 
4

2 回答 2

18

您必须解析对象中传入的 json 字符串。在此之后,您可以访问属性。

import json   # Now you can import json instead of simplejson
....
jsonstring = self.request.body
jsonobject = json.loads(jsonstring)
于 2012-08-23T12:19:32.893 回答
1
import cgi
import webapp2
import datamethods

from google.appengine.ext.webapp.util import run_wsgi_app

class adduser(webapp2.RequestHandler):
    def get(self):
        items = [] 
        response = { }

        userphone = self.request.get("phone") 
        name = self.request.get("name")

        items.append({'userphone': userphone , 'name':name})
        response['userInformation'] = items
        return response #return json data


app = webapp2.WSGIApplication([
  ('/adduser', adduser),
  ('/sign', updatestatus),
  ('/login',login)
], debug=True)


def main():
    run_wsgi_app(app)

if __name__ == "__main__":
    main()
于 2013-05-28T11:43:54.763 回答