# 对报文参数进行处理
def unicode_convert(input):
if isinstance(input, dict):
return {unicode_convert(key): unicode_convert(value) for key, value in input.iteritems()}
elif isinstance(input, list):
return [unicode_convert(element) for element in input]
elif isinstance(input, unicode):
return input.encode('utf-8')
else:
return input
import json
import datetime
class DateEncoder(json.JSONEncoder):
"""
json 无法序列化datetime类型,自定义进行序列化
"""
def default(self, obj):
if isinstance(obj, datetime.datetime):
return obj.strftime('%Y-%m-%d %H:%M:%S')
else:
return json.JSONEncoder.default(self, obj)
# 使用
s = datetime.datetime.now()
print(type(s))
test = {"time": s, "life": [11, 22, 33]}
print(json.dumps(test, cls=DateEncoder))