How to dump a Python dictionary to JSON when keys are non-trivial objects?

后端 未结 4 1091
深忆病人
深忆病人 2021-02-08 05:54
import datetime, json
x = {\'alpha\': {datetime.date.today(): \'abcde\'}}
print json.dumps(x)

The above code fails with a TypeError since

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-08 06:12

    http://jsonpickle.github.io/ might be what you want. When facing a similar issue, I ended up doing:

    to_save = jsonpickle.encode(THE_THING, unpicklable=False, max_depth=4, make_refs=False)
    

提交回复
热议问题