如何JSON EN code实体?实体、JSON、EN、code

2023-09-11 00:39:00 作者:惊呆者

我开始使用谷歌应用程序引擎。我想使AJAX聊天Twitter之类的。

I'm getting started with Google App Engine. I want to make AJAX chat like Twitter.

class ChatMessage(db.Model):
  message = db.StringProperty()
  created = db.DateTimeProperty(auto_now=True)

服务器JSON EN codeS响应,

Server JSON encodes the response,

class RPCHandler(webapp.RequestHandler):
  def get(self):
    chat_list = {'message':'Hello!'}
    self.response.out.write(simplejson.dumps(chat_list))

结果:你好!

Result: Hello!

这是确定。但更换RPCHandler

This is OK. But replace RPCHandler

class RPCHandler(webapp.RequestHandler):
  def get(self):
    newchat = ChatMessage(message="Hi!")
    newchat.put()
    que = db.Query(ChatMessage).order('-created')
    chat_list = que.fetch(limit=1)

    self.response.out.write(simplejson.dumps(chat_list))

结果:错误。该服务器是无法访问的(得)

Result: Error. The server is not accessible (get)

我怎样才能JSON EN code实体?

How can I JSON encode Entities?

推荐答案

在App Engine的Python中,你可以使用这个脚本来连接code db.Models以JSON。您可能需要定制一些地方,如日期时间格式。

In App Engine Python you can use this script to encode db.Models to JSON. You may have to customize some parts, like the DateTime formatting.

的http://$c$c.google.com/p/google-app-engine-samples/source/browse/trunk/geochat/json.py?r=55

 
精彩推荐