我正在使用Python快速完成一个小项目,尝试将一个对象编码成JSON字符串。以前我已经做过几次了,没有任何问题,但现在不行了。通常我只需要按照以下方式操作。
def ClassToEncode :
def __init__(self, arg1, arg2, ..., argn) :
self.attr1 = arg1
self.attr2 = arg2
...
self.attrn = argn
...
def toJSON(self) :
return json.dumps(self, default=lambda o: o.__dict__)
但问题在于,我的一个类属性是datetime.datetime对象,我遇到了以下错误。
AttributeError: 'datetime.datetime' object has no attribute '__dict__'
有没有什么想法或方法可以实现将日期时间属性包含在JSON输出中的功能?提前感谢!