直接上代码
自定义的编码解码函数,这里以时间类型的数据编码解码为例
def json_decode_datetime(obj):
if isinstance(obj, datetime):
return {
"json_decode_datetime": datetime.timestamp(obj)
}
return obj
def json_encode_datetime(dic):
if dic.get("json_decode_datetime", None):
return datetime.fromtimestamp(dic['json_decode_datetime'])
return dic
json调用编码解码函数
dict_data = json.loads(cache, object_hook=json_encode_datetime)
res_str = json.dumps(frame_full_info.dict(), default=json_decode_datetime)
本文介绍了一种自定义的时间类型JSON编解码方法。通过定义json_decode_datetime和json_encode_datetime两个函数,实现了将时间类型的数据转换为JSON格式并从JSON格式中还原出来。这种方法可以确保时间数据在存储和传输过程中的正确性和一致性。
2402

被折叠的 条评论
为什么被折叠?



