解决python报错Object of type xxx is not JSON serializable
Object of type Ip xxx is not JSON serializable其中xxx为自定义类的对象,调用json.dumps报了错。怎么解决呢? 很简单,将对象改为dict结构再转就OK了!示例:要转的对象为objconvert = dict()convert ['a'] = obj.aconvert ['b'] = obj.bresult = json.dumps(conve
·
Object of type Ip xxx is not JSON serializable
其中xxx为自定义类的对象,调用json.dumps报了错。
怎么解决呢? 很简单,将对象改为dict结构再转就OK了!
示例:要转的对象为obj
convert = dict()
convert ['a'] = obj.a
convert ['b'] = obj.b
result = json.dumps(convert)
常用类型到json的转换规则如下:
Python | JSON |
---|---|
dict | object |
list, tuple | array |
str, unicode | string |
int, long, float | number |
True | true |
False | false |
None | null |
更多推荐
已为社区贡献1条内容
所有评论(0)