On Fri, 2012-01-06 at 13:12 -0800, Andre Smit wrote:
> This has me stumped. I'm trying to apply the function as posted and
> trying to get rid of the trailing comma to ensure valid json. The
> following doesn't work:
>  
> def GET(self): 
>   web.header('Content', 'text/json') 
>   rows = db.select('foo') 
>   lrows = len(rows)
>   yield '[' 
>   for i,row in enumerate(rows): 
>     if i < lrows:
>        yield json.dumps(row) + ', ' 
>     else:
>        yield json.dumps(row)
>   yield ']' 

Try this:

    # I think this the correct header
    web.header('Content-type', 'application/json') 

    # First convert rows into a list of JSON dumps
    json_rows = [json.dumps(row) for row in db.select('foo')]

    # Then just join them.
    return '[%s]' % ', '.join(json_rows)




-- 
You received this message because you are subscribed to the Google Groups 
"web.py" group.
To post to this group, send email to webpy@googlegroups.com.
To unsubscribe from this group, send email to 
webpy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/webpy?hl=en.

Reply via email to