[Python]json 错误xx is not JSON serializable

TypeError: Decimal('1457501') is not JSON serializable

在使用json的时候经常会遇到xxx  is not JSON serializable,也就是无法序列化某些对象

import  decimal

class DecimalEncoder(json.JSONEncoder):

  def default(self, o):

    if isinstance(o, decimal.Decimal):

      return float(o)

    super(DecimalEncoder, self).default(o)

# and then:

json.dumps(chart_list,..., cls=DecimalEncoder)

猜你喜欢

转载自www.cnblogs.com/hanjiajiejie/p/9223511.html