import json
class Animal(object):
def __init__(self):
self.name = 'tom'
def __repr__(self):
return f'my name is {self.name}&i like apple'
d1 = {
'county': 'china',
'name': Animal()
}
class MyDefault(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, Animal):
return str(obj)
return json.JSONEncoder.default(self, obj)
print(json.dumps(d1, cls=MyDefault))
- serializable TypeError 对象 Object Animalserializable typeerror对象object serializable orderdict typeerror datetime caseinsensitivedict serializable头部typeerror caseinsensitivedict序列serializable typeerror serializable python object float 饲养员animal老虎 对象 typeerror nonetype python object bytes-like typeerror required object typeerror callable错误object subscriptable typeerror dict_keys object