如何JSON EN code实体? [英] How to JSON encode Entities?

查看:172
本文介绍了如何JSON EN code实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用谷歌应用程序引擎。我想使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

这篇关于如何JSON EN code实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆