Word definition JSON serialization supports DateTime format serialization
Because Json.dumps cannot handle datetime dates, it can be extended by customizing the processor, such as:
ImportJson fromDatetimeImportDate fromDatetimeImportDatetimeclassCustomencoder (JSON. Jsonencoder):defDefault Self, field):# Custom Section # return definition of strftime if isinstance(Field, datetime):returnField.strftime ('%y-%m-%d%h:%m:%s ')elif isinstance(field, date):returnField.strftime ('%y-%m-%d')Else:returnJson. Jsonencoder.default ( Self, field)if __name__ == ' __main__ ': DIC={' K1 ':' v1 ',' K2 ':123,' CTime ': DateTime.Now ()}# Call Method Json.dumps (dic variable, cls=customencoder)Ds=Json.dumps (DIC, CLS=Customencoder)Print(DS)
Output:
{"k1""v1""k2"123"ctime""2018-01-04 14:53:13"}
Word definition JSON serialization supports DateTime format serialization