OperetionJson.py
#!/usr/bin/python3
# -*- coding:utf-8 -*-
#Author:qika
import requests
import json
#封装操作json文件的方法
class OperetionJson:
def __init__(self,file_path=None):
if file_path == None:
self.file_path = '../Config/user.json'
else:
self.file_path = file_path
#读
def read_data(self):
with open(self.file_path) as fp:
data = json.load(fp) #文件读取(可详细查看一下序列化和反序列化,文件读写的介绍:https://www.cnblogs.com/QiKa/p/12900181.html)
return data
#读:并且根据key来获取数据
def get_data(self,key):
print(type(self.read_data()))
return self.read_data()[key]
#写
def write_data(self,data):
with open('../Config/user.json','w') as fp:
data1 =json.dump(data,fp) #文件写入:json.dump()
return data1
if __name__ == '__main__':
op_json = OperetionJson()
print(op_json.read_data()) #直接读取
print(op_json.get_data('PHPSESSID')) #根据文件内的key,来获取值