0
我想建立與Python和谷歌應用程序引擎REST服務,並有下面的代碼:JSON響應與Python
編輯代碼:
import webapp2
from google.appengine.ext import db
from google.appengine.api import users
import json
class Item(db.Model):
author = db.UserProperty(required=False)
summary = db.StringProperty(required=True)
description = db.StringProperty(multiline=True)
url = db.StringProperty()
created = db.DateTimeProperty(auto_now_add=True)
updated = db.DateTimeProperty(auto_now=True)
dueDate = db.StringProperty(required=True)
finished = db.BooleanProperty()
class GetAllItems(webapp2.RequestHandler):
def get(self):
item = Item(summary="Summary", dueDate="Date")
item.put()
allItems = Item.all()
data = []
for entry in allItems:
data.append(db.to_dict(entry))
self.response.out.write(json.dumps(entry))
app = webapp2.WSGIApplication(
[
('/api/items', GetAllItems)
],
debug=True)
我怎樣才能將所有物品將此模型轉換爲JSON並將其作爲JSON發回? 我總是得到這個錯誤:
TypeError: <main.Item object at 0x0538B590> is not JSON serializable
隨着我的模型,現在我得到這個錯誤:類型錯誤:不JSON可序列化 –
ManzMoody
你能告訴我你的代碼嗎? – masnun
我在 – ManzMoody