Python: Convert a list/tuple/dictionary into JSON

0 Comments

Django has a nice serializer library for models, but for a simple way of dumping primitive types straight out to JSON would be to use json.

If you try to use Django's serializer on primitives you'll get this error:

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

Instead, use Python's built in library support.

1.json.dumps(varname);

Python 2.5 has "simplejson", so to make sure you import the right one, just use this import statement.

1.try:
2.  import json
3.except ImportError:
4.  import simplejson as json

[ Source ]

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog