我正在使用dict來創建一個json對象,但我遇到了dict和json的一些問題!Python字典json
def get(self):
players = db.GqlQuery("SELECT * FROM Player")
playerInfo = {}
for player in players:
email = player.email.encode("utf-8")
gem = str(player.gem)
print email
print gem
playerInfo["email"] = email
playerInfo["gem"] = gem
b = json.dumps(playerInfo)
self.response.out.write(b)
出於某種原因,我只收到一個for循環,並且當我在for循環打印電子郵件我接收到6分的結果,但是playerInfo的輸出只具有1組數據。
{"email": "test1", "gem": "0"}
我預期的結果應該是
{"email": "test1", "gem": "0"},{"email": "test2", "gem": "2"}...
您並未創建列表;你只需要爲每個玩家重寫一個字典中的值,然後輸出最後一個值。 – geoffspear 2012-03-19 17:24:12