以下示例
将python中的数据结构:
字符串,数字,时间,列表,字典都序列化为json数据
import json from datetime import datetime from datetime import date info = { "name": "Tom", "age": 18, "time": datetime.now(), 'hobbies': ['music', 'read', 'dancing'], 'addr': { 'country': 'China', 'city': 'shanghai' } } class MyJsonEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, datetime): return obj.strftime('%Y-%m-%d %H:%M:%S') elif isinstance(obj, date): return obj.strftime('%Y-%m-%d') else: return json.JSONEncoder.default(self, obj) json_info=json.dumps(info, cls=MyJsonEncoder) print(json_info) """ { "name": "Tom", "age": 18, "time": "2019-09-14 21:47:29", "hobbies": ["music", "read", "dancing"], "addr": {"country": "China", "city": "shanghai"} } """
参考
</div>