我正在尝试使用具有继承性的python类重新创建以下JSON:
{"attribute1": "c1"
"attribute2": {"attribute3" : "test"}
}
class class1():
def __init__(self,attribute1):
self.attribute1 = attribute1
class class2(class1):
def __init__(self,attribute2):
class1.__init__(self,'test')
self.attribute2 = attribute2
c1 = class1('c1')
c2 = class2(c1)
print(json.dumps(c1.__dict__))
渲染:
{"attribute1": "c1"}
如果我尝试将变量c2
转换为JSON:
print(json.dumps(c2.__dict__))
我收到错误消息:
TypeError: Object of type class1 is not JSON serializable
class1
不可序列化,因为我之前使用print(json.dumps(c1.__dict__))
进行了转换