Fix: TypeError: Object of type 'Decimal' is not JSON serializable in Python 3.6

Python 28 มี.ค. 2019

Use a override default:

import json
from decimal import Decimal

def default(obj):
    if isinstance(obj, Decimal):
        return str(obj)
    raise TypeError("Object of type '%s' is not JSON serializable" % type(obj).__name__)

json.dumps(testlist, default=default)

ref: https://stackoverflow.com/questions/31202956/json-typeerror-decimal34-3-is-not-json-serializable

แท็ก

Onyx

Just a middle-aged programmer, Can do many things but not the most.