如何序列化包含其他对象列表的对象?
import json
class Foo:
def __init__(self, x, y):
self.x = x
self.y = y
class Bar:
def __init__(self):
self.z = 0
self.foos = []
bar = Bar()
bar.foos.append(Foo(1, 2))
print(json.dumps(bar.__dict__))
输出
TypeError: <__main__.Foo instance at 0x024DD5F8> is not JSON serializable