I tried to use this soln (which uses this file) to convert GAE db model to json. But when I tried to use it, I got this error "TypeError: datetime.date(2010, 7, 27) is not JSON serializable"
Does anyone know whats the problem?
Or, if 开发者_如何学Pythonyou know alternative soln to convert GAE db model to JSON please suggest so.
Looks like you need to modify the json.py you referenced and add a block to handle the type.
Look at how datetime.datetime
is handled at line 61:
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
You need to add something to handle datetime.date
:
elif isinstance(obj, datetime.date):
#your code here...
Or just use datetime.datetime
instead of datetime.date
.
精彩评论