How to make simplejson serializable class
问题 I have a class defined like this class A: def __init__(self): self.item1 = None def __repr__(self): return str(self.__dict__) when I do: >>> import simplejson >>> myA = A() >>> simplejson.dumps(myA) TypeError: {'item1': None} is not JSON serializable I can't find the reason why. Do I need to add any particular method to A for simplejson to serialize my class object? 回答1: You can't serialize arbitrary objects with simplejson . You need to pass a default and object_hook to dump and load . Here