1. ホーム
  2. python

[python][scrapy] 型 'bytes' のオブジェクトは JSON シリアライズ可能ではない

2022-02-13 18:23:03
#! /usr/bin/env python  
# -*- coding: utf-8 -*-  
#json_extention  
#2014-03-16  
#copyright: orangleliu  
#license: BSD  
  
''''' 
The dumps method in python is very useful, it can directly serialize our dict into json objects. 
But sometimes we can't serialize it if we add some custom classes, so we need to 
custom serialization methods 
 
Reference: 
http://docs.python.org/2.7/library/json.html 
 
For example: 
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))
  
''''' 
Output result. 
 
PS D:\code\python\python_abc> python . \json_extention.py 
{"date": "2014-03-16 13:56:39.003000", "num": 1112} 
'''  
  
# Let's try a custom class  
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))
--------------------------------------------------------------------
class MyEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, bytes):
            return str(obj, encoding='utf-8')
        return json.JSONEncoder.default(self, obj)
jsontext = json.dumps(dict(item),ensure_ascii=False,cls=MyEncoder)+'\n'
--------------------------------------------------------------------
''''' 
PS D:\code\python\python_abc> python . \json_extention.py 
{"date": "2014-03-16 14:01:46.738000", "num": 1112} 
{"user": "orangle"} 
 
'''  




--------------------------------------------------------------------

class MyEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, bytes):
            return str(obj, encoding='utf-8')
        return json.JSONEncoder.default(self, obj)

jsontext = json.dumps(dict(item),ensure_ascii=False,cls=MyEncoder)+'\n'

--------------------------------------------------------------------

''''' 
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関数にカスタムハンドラを追加して使用します。