Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在appengine中解析json格式的请求_Python_Google App Engine - Fatal编程技术网

Python 在appengine中解析json格式的请求

Python 在appengine中解析json格式的请求,python,google-app-engine,Python,Google App Engine,最近一直在开发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

最近一直在开发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() 

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

import json   # Now you can import json instead of simplejson
....
jsonstring = self.request.body
jsonobject = json.loads(jsonstring)

太棒了……谢谢。。。。我使用了内置的json库,比如:import json类消息(webapp.RequestHandler):def post(self):self.response.headers['Content-Type']=“text/plain”#self.response.out.write(self.request.body)data=json.load(self.request.body)self.response.out.write(data['sux'])这就解决了问题…你真的还需要简单的json吗,为什么不只是json呢?因为Python SDK版本1.9.55。您可以使用ujson。UltraJSON是一种用纯C编写的超高速JSON编码器和解码器
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()