json.dumps(),json.dump,json.loads,json.load的使用
import json d1 = [1, 2, ‘abc‘, {‘name‘: ‘s‘, ‘age‘: 18}] d2 = {‘b‘: None, ‘a‘: 1, ‘c‘: ‘cc‘} ‘‘‘dumps()与dump()区别在于接口的不同,dump()是对文件的写操作‘‘‘ print json.dumps(d1) print json.dumps(d2) print json.dumps(d2, separators=[‘,‘, ‘:‘]) print json.dumps(d2, sort_keys=True) with open(‘jsonName.json‘, ‘wb‘) as f: json.dump(d2, f) ‘‘‘loads()与load()区别在于接口的不同,load()是对文件的读操作‘‘‘ print json.loads(‘{"a": 1, "c": "cc", "b": null}‘) with open(‘jsonName.json‘, ‘rb‘) as f1: print json.load(f1)
时间: 2024-11-03 21:15:08