Possible duplicate:
Python serializable json objects
I need to know how to convert a dynamic python object to JSON. An object must be able to have several children of level objects. For example:
class C(): pass class D(): pass c = C() c.dynProperty1 = "something" c.dynProperty2 = { 1, 3, 5, 7, 9 } cd = D() cddynProperty3 = "d.something"
Using python 2.6 the following code:
import json class C(): pass class D(): pass c = C() c.what = "now?" c.now = "what?" cd = D() cdwhat = "d.what" json.dumps(c.__dict__)
produces the following error:
TypeError: <__main__.D instance at 0x99237ec> is not JSON serializable
I do not know what types of subobjects a user can insert in c . Is there a solution smart enough to determine if an attribute is an object and its parsing is __dict__ automatically?
UPDATED to include subobjects on c .
json python
Trevor Sep 13 '11 at 21:18 2011-09-13 21:18
source share