views:

389

answers:

2

I am sending information between client and django server and I would like to use json to this. I am sending simple information - list of strings. I tried using django.core.serializers, but when I did, I got

AttributeError: 'str' object has no attribute '_meta'

It seems, this can be used only for django objects. How can I serialize simple, python objects?

+3  A: 

You can use pure Python to do it:

import json
list = [1, 2, (3, 4)] # Note that the 3rd element is a tuple (3, 4)
json.dumps(list) # '[1, 2, [3, 4]]'
Emil Ivanov
Oh, it's a new module in 2.6, I don't have to use simplejson. Thanks a lot :-)
gruszczy
+2  A: 

Just a note: if using python 2.5 you may need to import simplejson:

try:
    import json
except ImportError:
    import simplejson as json
reech
That's a good idea. Thanks :-)
gruszczy