刘珲的博客

导航

django解决Object of type Decimal is not JSON serializable

class DecimalEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, decimal.Decimal):
return float(o)
super(DecimalEncoder, self).default(o)

下边进行调用上边定义好的方法就好了

json.dumps(price, cls= DecimalEncoder)

posted on 2020-08-28 14:24  刘珲的博客  阅读(491)  评论(0编辑  收藏  举报