Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok - my python not great so any help would be appreciated - You dont need to write a parser - this is the solution:</p> <p>add this utlity class <a href="http://code.google.com/p/google-app-engine-samples/source/browse/trunk/geochat/json.py?r=55" rel="noreferrer">http://code.google.com/p/google-app-engine-samples/source/browse/trunk/geochat/json.py?r=55</a> </p> <pre><code> import datetime import time from google.appengine.api import users from google.appengine.ext import db #this is a mod on the orinal file for some reason it includes its own simplejson files i have ref django! from django.utils import simplejson class GqlEncoder(simplejson.JSONEncoder): """Extends JSONEncoder to add support for GQL results and properties. Adds support to simplejson JSONEncoders for GQL results and properties by overriding JSONEncoder's default method. """ # TODO Improve coverage for all of App Engine's Property types. def default(self, obj): """Tests the input object, obj, to encode as JSON.""" if hasattr(obj, '__json__'): return getattr(obj, '__json__')() if isinstance(obj, db.GqlQuery): return list(obj) elif isinstance(obj, db.Model): properties = obj.properties().items() output = {} for field, value in properties: output[field] = getattr(obj, field) return output elif isinstance(obj, datetime.datetime): output = {} fields = ['day', 'hour', 'microsecond', 'minute', 'month', 'second', 'year'] methods = ['ctime', 'isocalendar', 'isoformat', 'isoweekday', 'timetuple'] for field in fields: output[field] = getattr(obj, field) for method in methods: output[method] = getattr(obj, method)() output['epoch'] = time.mktime(obj.timetuple()) return output elif isinstance(obj, time.struct_time): return list(obj) elif isinstance(obj, users.User): output = {} methods = ['nickname', 'email', 'auth_domain'] for method in methods: output[method] = getattr(obj, method)() return output return simplejson.JSONEncoder.default(self, obj) def encode(input): """Encode an input GQL object as JSON Args: input: A GQL object or DB property. Returns: A JSON string based on the input object. Raises: TypeError: Typically occurs when an input object contains an unsupported type. """ return GqlEncoder().encode(input) </code></pre> <p><strong>save as json.py</strong></p> <p>TO USE</p> <pre><code>import cgi import os import json from google.appengine.ext.webapp import template from google.appengine.api import users from google.appengine.ext import webapp from google.appengine.ext.webapp.util import run_wsgi_app from google.appengine.ext import db class Greeting(db.Model): author = db.UserProperty() content = db.StringProperty(multiline=True) date = db.DateTimeProperty(auto_now_add=True) class MainPage(webapp.RequestHandler): def get(self): greetings_query = Greeting.all().order('-date') greetings = greetings_query.fetch(5) if users.get_current_user(): url = users.create_logout_url(self.request.uri) url_linktext = 'Logout' else: url = users.create_login_url(self.request.uri) url_linktext = 'Login' template_values = { 'greetings': greetings, 'url': url, 'url_linktext': url_linktext, } path = os.path.join(os.path.dirname(__file__), 'index.html') self.response.out.write(template.render(path, template_values)) class Guestbook(webapp.RequestHandler): def post(self): greeting = Greeting() if users.get_current_user(): greeting.author = users.get_current_user() greeting.content = self.request.get('content') greeting.put() self.redirect('/') #here i return my json feed - simple implementaion for example class FeedHandler(webapp.RequestHandler): def get(self): """Retrieve a feed""" user = None greetings_query = Greeting.all().order('-date') rs= greetings_query.fetch(5) #this is the part that calls the encoder - dosnt cause an exception data = json.encode(rs) #roll out to browser -might need to check my headers etc self.response.headers['Content-Type'] = 'application/json; charset=utf-8' self.response.out.write(data) application = webapp.WSGIApplication( [ ('/', MainPage), ('/sign',Guestbook), ('/feed',FeedHandler), ], debug=True ) def main(): run_wsgi_app(application) if __name__ == "__main__": main() </code></pre> <p>This is the browser response:</p> <blockquote> <p>[{"content": "", "date": {"ctime": "Sat Jan 23 02:40:22 2010", "hour": 2, "isoweekday": 6, "month": 1, "second": 22, "microsecond": 434000, "isocalendar": [2010, 3, 6], "timetuple": [2010, 1, 23, 2, 40, 22, 5, 23, -1], "year": 2010, "epoch": 1264214422.0, "isoformat": "2010-01-23T02:40:22.434000", "day": 23, "minute": 40}, "author": {"nickname": "test@example.com", "email": "test@example.com", "auth_domain": "gmail.com"}}, {"content": "", "date": {"ctime": "Sat Jan 23 01:12:43 2010", "hour": 1, "isoweekday": 6, "month": 1, "second": 43, "microsecond": 972000, "isocalendar": [2010, 3, 6], "timetuple": [2010, 1, 23, 1, 12, 43, 5, 23, -1], "year": 2010, "epoch": 1264209163.0, "isoformat": "2010-01-23T01:12:43.972000", "day": 23, "minute": 12}, "author": {"nickname": "test@example.com", "email": "test@example.com", "auth_domain": "gmail.com"}}, {"content": "test", "date": {"ctime": "Fri Jan 22 22:32:13 2010", "hour": 22, "isoweekday": 5, "month": 1, "second": 13, "microsecond": 659000, "isocalendar": [2010, 3, 5], "timetuple": [2010, 1, 22, 22, 32, 13, 4, 22, -1], "year": 2010, "epoch": 1264199533.0, "isoformat": "2010-01-22T22:32:13.659000", "day": 22, "minute": 32}, "author": {"nickname": "test@example.com", "email": "test@example.com", "auth_domain": "gmail.com"}}, {"content": "", "date": {"ctime": "Fri Jan 22 22:29:49 2010", "hour": 22, "isoweekday": 5, "month": 1, "second": 49, "microsecond": 358000, "isocalendar": [2010, 3, 5], "timetuple": [2010, 1, 22, 22, 29, 49, 4, 22, -1], "year": 2010, "epoch": 1264199389.0, "isoformat": "2010-01-22T22:29:49.358000", "day": 22, "minute": 29}, "author": {"nickname": "test@example.com", "email": "test@example.com", "auth_domain": "gmail.com"}}, {"content": "ah it works!\r\n", "date": {"ctime": "Fri Jan 22 22:29:22 2010", "hour": 22, "isoweekday": 5, "month": 1, "second": 22, "microsecond": 995000, "isocalendar": [2010, 3, 5], "timetuple": [2010, 1, 22, 22, 29, 22, 4, 22, -1], "year": 2010, "epoch": 1264199362.0, "isoformat": "2010-01-22T22:29:22.995000", "day": 22, "minute": 29}, "author": {"nickname": "test@example.com", "email": "test@example.com", "auth_domain": "gmail.com"}}]</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload