django 中,json.dumps 无法直接转译 datetime 类型的值。

找了无数方法,找到一个最优、最简洁的解决办法:

json.dumps(results, indent=4, sort_keys=True, default=str)

indent=4:输出的json文件中,用4个空格表示间距。

sort_keys:输出的json文件中,次序使用key。

default=str:碰到 json.dumps 无法转译的类型值,采用直接转化为 str 类型再转译。

相关文章:

  • 2022-01-21
  • 2022-03-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-17
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-10-27
  • 2022-12-23
  • 2022-12-23
  • 2021-10-01
  • 2022-02-16
  • 2022-12-23
相关资源
相似解决方案