python操作json文件
1 import json 2 class OperationJson(object): 3 def __init__(self,file_name=None): 4 if file_name: 5 self.file_name=file_name 6 else: 7 self.file_name='E:/python/requests/dataconfig/request.json' 8 self.data=self.read_data() 9 #读取json文件,并将其反序列化为python对象 10 def read_data(self): 11 with open(self.file_name) as fp: 12 data=json.load(fp) 13 return data 14 #根据关键字获取数据 15 def get_data(self,id): 16 return self.data[id] 17 18 if __name__=='__main__': 19 js=OperationJson() 20 print(js.get_data('login'))