python 字典转化为json字符串中,由于Python一些自带的类型无法,进行json dump。比如datetime类型,
此时,需要重写 josn.JSONEncoder 的default方法
- 代码示例
class CoustomerJsonEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime.datetime):
return obj.strftime('%Y-%m-%dT%H:%M:%SZ')
elif isinstance(obj, Decimal):
return float(obj)
else:
return json.JSONEncoder.default(self, obj)
网友评论