How to overcome “datetime.datetime not JSON serializable”?

后端 未结 30 2709
梦谈多话
梦谈多话 2020-11-22 03:31

I have a basic dict as follows:

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


        
30条回答
  •  渐次进展
    2020-11-22 03:58

    You should use .strftime() method on .datetime.now() method to making it as a serializable method.

    Here's an example:

    from datetime import datetime
    
    time_dict = {'time': datetime.now().strftime('%Y-%m-%dT%H:%M:%S')}
    sample_dict = {'a': 1, 'b': 2}
    sample_dict.update(time_dict)
    sample_dict
    

    Output:

    Out[0]: {'a': 1, 'b': 2, 'time': '2017-10-31T15:16:30'}
    

提交回复
热议问题