Python 如何修改并存储 json文件内容 - json package 使用

发布时间 2023-12-15 11:51:12作者: Gloria_Chen

 

直接上代码:
import json

def json_load(json_file):
with open(json_file, 'r') as fh:
content = json.load(fh)
return content
fh.close()

def json_save(json_file, data):
with open(json_file,'w',encoding='UTF-8') as f:
json.dump(data, f)
f.close()

def modify_json():
json_file = 'C:\Python\\assets\config.json'
data = json_load(json_file)
data['auth']['clientId'] = "Test ClientID"
data['auth']['authority'] = "Test authority"
data['auth']['scopes'] = "Test scopes"
json_save(json_file, data)

if __name__ == '__main__':
modify_json()