作者eight0 (octō)
看板Python
标题[问题] json.JSONEncoder 与 dict
时间Sun Jun 7 20:00:04 2015
各位晚安
小弟我想把一些 pickle 档案转成 json,
其中有一些 hash table,想把 key 拿掉,转成 list。如
{
"key001": {
"key": 1,
...
},
"key002": {
"key": 2,
...
},
...
}
=>
[
{
"key": 1,
...
},
{
"key": 2,
...
},
...
]
我原本以为设 JSONEncoder 就能解决
class Encoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, dict) and check_key_prop(obj):
return list(obj.values())
return super().default(obj)
但完全没有作用,只有 unserializable 的物件才会传给 Encoder
不知道是不是我漏看了哪些参数
1. 原本官方设定就是这样吗?Encoder 只在不能 serialize 时才工作?
2. 还有其它比较建议的解决办法吗?
--
(* ̄▽ ̄)/‧★*"`'*-.,_,.-*'`"*-.,_☆,.-*`
http://i.imgur.com/oAd97.png
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 1.160.87.157
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1433678408.A.3E3.html
1F:→ uranusjr: 1. 对 2. 没有建议解法, 只能自己转或重写一个 06/07 23:17