import json
class OptertionJson:
def __init__(self, file_path=None):
if file_path == None:
self.file_path = ‘./test.json‘
else:
self.file_path = file_path
self.data = self.get_file()
‘‘‘
读取json配置文件
‘‘‘
def get_file(self):
with open(self.file_path) as fp:
json_data = fp.read()
return json.loads(json_data)
‘‘‘
根据关键字获取数据
‘‘‘
def get_value(self, key):
value = self.data[key]
return value
‘‘‘
写入数据
‘‘‘
def set_write(self, data):
with open(‘./test.json‘,‘w‘) as fp:
fp.write(json.dumps(data))
if __name__ == ‘__main__‘:
oj = OptertionJson()
s = oj.get_value(‘head‘)
print(s)
print(type(oj.data))
# oj.set_write("asdfasf")
原文:https://www.cnblogs.com/xinyueqingfeng/p/11725863.html