读取json数据封装
前言:做自动化常用公共方法
测试JSON数据
{
"2": {"username": "13399999999","password":"123456"},
"3":{"endEnterTime":"","gradeId":"b462ebf4-2fed-4dfe-a4bf-926c6e551273","groupId":"","inviteMobile":"","merchantId":"","mobile":"","name":"","pageNumber":1,"pageSize":20,"salesmanNameOrMobile":"","startEnterTime":""},
"4": {
"groupId":"660ea9e3-faf2-42ea-9c6b-f866950d637c","invitationMobile":"13511116668","message":false,"salesmanMobile":"13511116669","salesmanName":"asdfg"
},
"5": {"id":"$name"},
"6": {"pageSize":10,"pageNumber":1},
"7": {"groupId":"$groupID"}
}
代码实现
import json import os class ReadJson(object): def __init__(self, path): self.data = self.read_data(path) #读取JSON数据 def read_data(self, path): with open(os.getcwd() + path) as fp: data = json.load(fp) return data def get_data(self, key=None): """根据关键字获取数据""" if key == None: return self.data return self.data[key] if __name__ == '__main__': data = ReadJson('/test.json') print(data) data1 = data.get_data() data2 = data.get_data('7') print(data1) print(data2)
打印结果