Created
June 22, 2011 12:46
-
-
Save sweemeng/1040013 to your computer and use it in GitHub Desktop.
Output Objects To JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
""" | |
>>> c = C() | |
>>> c.a = 1 | |
>>> c.b = 2 | |
>>> c.__dict__ | |
{'a':1,'b':2} | |
>>> c.to_json() | |
'{"a":1,"b":2}' | |
>>> json.dumps(c.__dict__) | |
'{"a":1,"b":2}' | |
""" | |
class C: | |
def to_json(self): | |
json.dumps(self.__dict__) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment