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)

相关文章:

  • 2022-12-23
  • 2021-10-01
  • 2022-02-17
  • 2022-12-23
  • 2022-12-23
  • 2021-10-27
  • 2021-09-04
  • 2021-06-09
猜你喜欢
  • 2022-12-23
  • 2022-02-16
  • 2022-12-23
  • 2022-03-01
  • 2022-12-23
  • 2021-09-01
  • 2022-01-21
相关资源
相似解决方案