>>> import json
# 序列化列表对象
# 直接查看序列化后的结果
>>> json.dumps(['a','b','c'])
'["a", "b", "c"]'
# 反序列化
>>> json.loads...(_)
['a', 'b', 'c']
# 序列化字典对象
>>> json.dumps({'a':1, 'b':2, 'c':3})
'{"a": 1, "b": 2, "c": 3}'
>>> json.loads...__name__)
TypeError: Object of type 'set' is not JSON serializable
# 自定义序列化编码器
>>> class setEncoder(...setDecoder(json.JSONDecoder):
def decode(self, obj):
return set(json.JSONDecoder.decode(self, obj))
# 使用自定义的编码器和解码器...>>> with open('test.txt', 'w') as fp:
json.dump(s, fp)
# 读取文件内容并反序列化
>>> with open('test.txt') as