我正在尝试为我的网络应用创建一个API,但在尝试将GeoPtProperty加载到JSON时,我一直收到错误消息。我尝试将GeoPt数据转换为JSON对象,但它不起作用。
class Post(ndb.Model):
content = ndb.StringProperty(required = True)
user = ndb.StringProperty()
coords = ndb.GeoPtProperty()
def render(self):
self._render_text = self.content.replace('\n', '<br>')
params = dict(p = self)
return render_str("post.html", **params)
def as_dict(self):
d = {'content': self.content,
'user': self.user,
'coords': self.coords}
return d
class Handler(webapp2.RequestHandler):
def write(self, *a, **kw):
self.response.out.write(*a, **kw)
def render_json(self, d):
json_txt = json.dumps(d)
self.response.headers['Content-Type'] = 'application/json; charset=UTF-8'
self.write(json_txt)
class MainPage(Handler):
def get(self):
posts = Post.query(ancestor=post_key()).order(-Post.created)
if self.format == 'json':
self.response.headers['Access-Control-Allow-Origin'] = '*'
return self.render_json([p.as_dict() for p in posts])
以下是追溯
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~website/1.396738651304801066/main.py", line 121, in get
return self.render_json([p.as_dict() for p in posts])
File "/base/data/home/apps/s~website/1.396738651304801066/main.py", line 45, in render_json
json_txt = json.dumps(d)
File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/json/__init__.py", line 243, in dumps
return _default_encoder.encode(obj)
File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/json/encoder.py", line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/json/encoder.py", line 270, in iterencode
return _iterencode(o, 0)
File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/json/encoder.py", line 184, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: datastore_types.GeoPt(37.636003000000002, 127.216528) is not JSON serializable
如何正确接收JSON数据?
答案 0 :(得分:0)
我终于明白了,并将回答我自己的问题。 GeoPt数据无法序列化,因此必须将其转换为JSON对象。存储GeoPT时,您只需使用以下代码即可。
if coords:
coords = ndb.GeoPt(lat, lon)
p.coords = coords
p.put()
ndb数据存储区实体如下所示:
class Post(ndb.Model):
coords = ndb.GeoPtProperty()
我能够获取lat和lon,并将它们转换为JSON对象。
coords = self.coords.lat, self.coords.lon
lat, lon = coords[0], coords[1]
coords = {'latitude': lat, 'longitude': lon}
这样做了,我的应用程序不再抛出错误。