On Mar 24, 11:05 am, gchuyun <gchu...@gmail.com> wrote:
> i tried this, but get a error similar.
>
> TypeError: [{'publish_time': datetime.datetime(2009, 3, 24, 16, 16,
> 18), 'is_deleted': 0, 'is_confirmed'
> : 0, 'title': u'1', 'content': u'111', 'id': 1L}, {'publish_time':
> datetime.datetime(2009, 3, 24, 16
> , 27, 50), 'is_deleted': 0, 'is_confirmed': 0, 'title': u'12',
> 'content': u'22', 'id': 2L}, {'publish_time'
> : datetime.datetime(2009, 3, 24, 16, 28, 50), 'is_deleted': 0,
> 'is_confirmed': 0, 'title': u'ssssssss'
> , 'content': u'dddddddddddddddd', 'id': 3L}] is not JSON serializable
>


The real problem is not a list of dictionaries structure but datetime
field. Check this out:

CODE:
my_list = [
        {'publish_time': datetime.datetime(2009, 3, 24, 16, 16,18),
'is_deleted': 0, 'is_confirmed': 0, 'title': u'1', 'content': u'111',
'id': 1L},
        {'publish_time': datetime.datetime(2009, 3, 24, 16, 27, 50),
'is_deleted': 0, 'is_confirmed': 0, 'title': u'12', 'content': u'22',
'id': 2L},
        {'publish_time': datetime.datetime(2009, 3, 24, 16, 28, 50),
'is_deleted': 0,'is_confirmed': 0, 'title': u'ssssssss', 'content':
u'dddddddddddddddd', 'id': 3L}
]

print simplejson.dumps(my_list)

OUTPUT:
...
TypeError: datetime.datetime(2009, 3, 24, 16, 16, 18) is not JSON
serializable


So to solve this problem you may represent datatime fields as strings:

def make_it_json_friendly(element):
        element['publish_time'] = str(element['publish_time'])
        return element

my_list = map(make_it_json_friendly, my_list)

print simplejson.dumps(my_list)

I hope this will help.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to