[Python]json 错误xx is not JSON serializable
2014-03-16 14:06
435 查看
在使用json的时候经常会遇到xxx is not JSON serializable,也就是无法序列化某些对象。经常使用django的同学知道django里面有个自带的Encoder来序列化时间等常用的对象。其实我们可以自己定定义对特定类型的对象的序列化,下面看下怎么定义和使用的。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#json_extention
#2014-03-16
#copyright: orangleliu
#license: BSD
'''
python中dumps方法很好用,可以直接把我们的dict直接序列化为json对象
但是有的时候我们加了一些自定义的类就没法序列化了,这个时候需要
自定义一些序列化方法
参考: http://docs.python.org/2.7/library/json.html
例如:
In [3]: from datetime import datetime
In [4]: json_1 = {'num':1112, 'date':datetime.now()}
In [5]: import json
In [6]: json.dumps(json_1)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
D:\devsofts\python2.7\lib\site-packages\django\core\management\commands\shell.py
c in <module>()
----> 1 json.dumps(json_1)
TypeError: datetime.datetime(2014, 3, 16, 13, 47, 37, 353000) is not JSON serial
izable
'''
from datetime import datetime
import json
class DateEncoder(json.JSONEncoder ):
def default(self, obj):
if isinstance(obj, datetime):
return obj.__str__()
return json.JSONEncoder.default(self, obj)
json_1 = {'num':1112, 'date':datetime.now()}
print json.dumps(json_1, cls=DateEncoder)
'''
输出结果:
PS D:\code\python\python_abc> python .\json_extention.py
{"date": "2014-03-16 13:56:39.003000", "num": 1112}
'''
#我们自定义一个类试试
class User(object):
def __init__(self, name):
self.name = name
class UserEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, User):
return obj.name
return json.JSONEncoder.default(self, obj)
json_2 = {'user':User('orangle')}
print json.dumps(json_2, cls=UserEncoder)
'''
PS D:\code\python\python_abc> python .\json_extention.py
{"date": "2014-03-16 14:01:46.738000", "num": 1112}
{"user": "orangle"}
'''
定义处理方法是继承json.JSONEncoder的一个子类,使用的时候是在dumps方法的cls函数中添加自定义的处理方法。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#json_extention
#2014-03-16
#copyright: orangleliu
#license: BSD
'''
python中dumps方法很好用,可以直接把我们的dict直接序列化为json对象
但是有的时候我们加了一些自定义的类就没法序列化了,这个时候需要
自定义一些序列化方法
参考: http://docs.python.org/2.7/library/json.html
例如:
In [3]: from datetime import datetime
In [4]: json_1 = {'num':1112, 'date':datetime.now()}
In [5]: import json
In [6]: json.dumps(json_1)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
D:\devsofts\python2.7\lib\site-packages\django\core\management\commands\shell.py
c in <module>()
----> 1 json.dumps(json_1)
TypeError: datetime.datetime(2014, 3, 16, 13, 47, 37, 353000) is not JSON serial
izable
'''
from datetime import datetime
import json
class DateEncoder(json.JSONEncoder ):
def default(self, obj):
if isinstance(obj, datetime):
return obj.__str__()
return json.JSONEncoder.default(self, obj)
json_1 = {'num':1112, 'date':datetime.now()}
print json.dumps(json_1, cls=DateEncoder)
'''
输出结果:
PS D:\code\python\python_abc> python .\json_extention.py
{"date": "2014-03-16 13:56:39.003000", "num": 1112}
'''
#我们自定义一个类试试
class User(object):
def __init__(self, name):
self.name = name
class UserEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, User):
return obj.name
return json.JSONEncoder.default(self, obj)
json_2 = {'user':User('orangle')}
print json.dumps(json_2, cls=UserEncoder)
'''
PS D:\code\python\python_abc> python .\json_extention.py
{"date": "2014-03-16 14:01:46.738000", "num": 1112}
{"user": "orangle"}
'''
定义处理方法是继承json.JSONEncoder的一个子类,使用的时候是在dumps方法的cls函数中添加自定义的处理方法。
相关文章推荐
- Python json 错误xx is not JSON serializable解决办法
- python2.7web代码用python3.6运行时,浏览器发送请求报错TypeError: Object of type 'bytes' is not JSON serializable的解决办法
- python datetime.datetime is not JSON serializable 报错问题解决
- python datetime.datetime is not JSON serializable 报错问题解决
- Django自定义数据段问题即 [] is not JSON serializable(数组非JSON序列化)
- python错误:TypeError: 'str' object is not callable
- python错误: TypeError:'dict' object is not callable
- eclipse下 CVS 错误 sticky tag 'XX' for file 'XX' is not a branch 解决方式
- python错误--'list' object is not callable可能的原因之一
- datetime.datetime() is not JSON serializable
- django object is not JSON serializable error问题解决
- datetime.datetime(2014, 1, 23, 21, 6, 37) is not JSON serializable
- datetimeTypeError: datetime.datetime(2015, 1, 29, 8, 17, 41) is not JSON serializable
- TypeError:a bytes-like object is requeired ,not 'int'(使用python3编写出现的错误)
- opencv-python cv2接口下错误TypeError: mat data type=18 is not supported
- Django笔记---项目报错之“xxx is not JSON serializable”
- Python 遇到NameError: name '_name_' is not defined这样的错误
- TypeError: ObjectId('') is not JSON serializable
- [scrapy] exceptions.TypeError:XXX is not json serializable
- TypeError: datetime is not JSON serializable