如何克服“ datetime.datetime无法JSON序列化”?

☆樱花仙子☆ 提交于 2020-05-09 21:20:34

问题:

I have a basic dict as follows: 我有一个基本的命令,如下所示:

sample = {}
sample['title'] = "String"
sample['somedate'] = somedatetimehere

When I try to do jsonify(sample) I get: 当我尝试执行jsonify(sample)我得到:

TypeError: datetime.datetime(2012, 8, 8, 21, 46, 24, 862000) is not JSON serializable

What can I do such that my dictionary sample can overcome the error above? 我该怎么做才能使我的词典示例可以克服上述错误?

Note: Though it may not be relevant, the dictionaries are generated from the retrieval of records out of mongodb where when I print out str(sample['somedate']) , the output is 2012-08-08 21:46:24.862000 . 注意:尽管可能不相关,但字典是从mongodb中检索记录生成的,当我打印出str(sample['somedate']) ,输出为2012-08-08 21:46:24.862000


解决方案:

参考一: https://stackoom.com/question/npQg/如何克服-datetime-datetime无法JSON序列化
参考二: https://oldbug.net/q/npQg/How-to-overcome-datetime-datetime-not-JSON-serializable
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!