问题:
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
来源:oschina
链接:https://my.oschina.net/u/3797416/blog/4272330