json.dumps 执行时会提示 TypeError:
(Pdb) cc == {'y': 17318, 'info_total_periods': 12} True (Pdb) cc is {'y': 17318, 'info_total_periods': 12} False (Pdb) json.dumps(cc) *** TypeError: 17318 is not JSON serializable (Pdb) json.dumps({'y': 17318, 'info_total_periods': 12}) '{"y": 17318, "info_total_periods": 12}'
为什么会这样?有什么解决方法没?
![]() | 1 misaka19000 2019-11-06 18:53:56 +08:00 == 是什么意思 |
![]() | 2 gwy15 2019-11-06 18:54:31 +08:00 ![]() 看一下 cc['y'].__class__,不一定是 int |
![]() | 3 hjq98765 OP |
![]() | 4 ipwx 2019-11-06 20:11:46 +08:00 |
![]() | 5 jdhao 2019-11-06 21:48:47 +08:00 via Android 非 python 原生类型可能不支持 dump, |
![]() | 6 ex2vkf 2019-11-06 21:51:15 +08:00 这个坑我也遇到过,也是 numpy 的锅 |
![]() | 7 xingheng 2019-11-07 13:00:03 +08:00 Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 29 2018, 20:59:26) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin >>> json.dumps({'y': 17318, 'info_total_periods': 12}) '{"y": 17318, "info_total_periods": 12}' >>> json.dumps(cc) '{"y": 17318, "info_total_periods": 12}' >>> 没有重现 |
![]() | 8 hjq98765 OP @xingheng #7 原文:“Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 29 2018, 20:59:26)[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin>>> json.dumps({'y': 17318, 'info_total_periods': 12})'{"y": 17318, "info_total_periods": 12}'>>> json.dumps(cc)'{"y": 17318, "info_total_periods": 12}'>>>没有重现” ====== 回复:cc 是用其它方式生成的,所以 17318 是 numpy.int64,直接手写那就还是 python 的内置整数类型 |