首页 > 编程语言 >Python学习笔记--json序列化时间报错-改源码

Python学习笔记--json序列化时间报错-改源码

时间:2023-04-24 18:34:23浏览次数:42  
标签:res datetime json 源码 报错 date 序列化 today

问题:转换时间报错

执行代码为:

import json
from datetime import  date,datetime
d = {"time1":date.today(),"time2":datetime.today()}
res = json.dumps(d) # 报错

 

 

TypeError: Object of type date is not JSON serializable

方案1:

  • 手动转换str()

方案2:

  • 继承类
  • import json
    from datetime import  date,datetime
    class MyJsonEncoder(json.JSONEncoder):
        def default(self, o:str):
            if isinstance(o,datetime):
                # print("找到2")
                return o.strftime("%Y-%m-%d %X")
            if isinstance(o,date):
                # print("找到1")
                return o.strftime("%Y-%m-%d")
            return o
    
    
    d = {"time1":date.today(),"time2":datetime.today()}
    print(type(date.today()),type(datetime.today()))
    # res = json.dumps(d) # 报错
    res = json.dumps(d,cls=MyJsonEncoder)
    print(res)

     

 

标签:res,datetime,json,源码,报错,date,序列化,today
From: https://www.cnblogs.com/liqi175/p/17350504.html

相关文章

  • 【c&c++】VScode报错error: ‘::main‘ must return ‘int‘ void main()
    在运行指针时终端出现error:‘::main’mustreturn‘int’voidmain()错误。源代码如下:#include<stdio.h>voidmain(){inta,*p,b,c,d,e;a=100;p=&a;/*(*&a)先进行&a运算,得a的地址,再进行*运算,即变量a的值*/b=*&a;printf("a=%d\n",a);......
  • ElementUI: Uncaught (in promise) cancel 报错
    场景:使用element confirm组件时,点击【取消】按钮,提示错误 Uncaught(inpromise)cancel 代码如下:open(){this.$confirm('此操作将永久删除该文件,是否继续?','提示',{confirmButtonText:'确定',cancelButtonText:'取消',......
  • pip 安装库是报错ERROR: Cannot unpack file C:\Users\LX\AppData\Local\Temp\p
    使用pip安装python库的时候出现报错:ERROR:CannotunpackfileC:\Users\LX\AppData\Local\Temp\pip-unpack-apk_4xkw\simple(downloadedfromC:\Users\LX\AppData\Local\Temp\pip-req-build-htbv29co,content-type:text/html;charset=utf-8);cannotdetectarch......
  • 开源外卖系统源码解析:如何快速搭建属于自己的订餐平台?
    随着外卖市场的日益壮大,许多商家和个人都在考虑如何搭建一个属于自己的订餐平台。而在这个过程中,开源外卖系统源码无疑是一项不可或缺的资源。本文将以“开源外卖系统源码解析:如何快速搭建属于自己的订餐平台?”为主题,向您介绍外卖系统的相关内容,帮助您更好地了解如何利用现有的开源......
  • 一文详解RocketMQ-Spring的源码解析与实战
    摘要:这篇文章主要介绍SpringBoot项目使用rocketmq-springSDK实现消息收发的操作流程,同时笔者会从开发者的角度解读SDK的设计逻辑。本文分享自华为云社区《RocketMQ-Spring:实战与源码解析一网打尽》,作者:勇哥java实战分享。RocketMQ是大家耳熟能详的消息队列,开源项目......
  • web前端pdf.js预览pdf实例创建报错:Array. prototype` contains unexpected enumerable
    使用pdf.min.js是预览pdf文件,但是在实例化时异常报错,下面是实例化的代码varloadingTask=window.pdfjsLib.getDocument(url);console.log(loadingTask);this.pageNum=1;this.pageRendering=false;this.pageNumPending=null;loadingTask.promise.then((pdfDoc_)=>......
  • uiautomatorviewer.bat 多种报错问题的解决办法
    问题一:使用Android_sdk--tools里的uiautomatorviewer.bat定位页面元素时报错:Remoteobjectdoesn'texists解决办法:使用uiautomatorviewer.bat时要关闭Appium。因为它们都使用同一个端口来连接模拟器。 问题二:使用uiautomatorviewer.bat定位页面元素时报错:Unexpected......
  • 视频直播源码,android动画小飞机旋转效果
    视频直播源码,android动画小飞机旋转效果 //小飞机旋转动效果publicclassPlaneViewextendsView{  privatePaintpaint;  privateintwidth;  privateintheight;  privatefloatcurLength;  privatefloatallLength;  privatefloatmAnimato......
  • springboot集成redis时总报错Connection refused: no further information: localhost
    nacos上配置的关于redis的key值不是springboot需要的固定写法如:sping.redis.host=spring.redis.port=sping.redis.password=spring.redis.database=我写的是一个自定义的key如com.dream.redis.host改为springboot认识的即可其他和springboot集成的组件类似,切记......
  • npm i 报错 unable to resolve dependency tree
    错误:问题原因:安装包各个版本冲突解决办法:npmi--legacy-peer-deps忽略各种报错命令npmi--legacy-peer-deps--ignore-scripts--registry=https://registry.npm.taobao.org然后重新安装 npminstall 或者 cnpmi ......