我正在使用python,我将json结构放入字典中,并将其导出到文件中。现在我需要从文件重新加载结构,我想将它重新加载到字典中(为了更新它)但我遇到了一些问题。这是我的代码:
#export the structure
with open('data.json','w') as f:
data = {}
data['test'] = '1'
f.write(json.dumps(data))
#reload the structure
with open('data.json','r') as f:
dict = {}
dict = json.loads(f.read())
错误是:无法解码JSON对象。
答案 0 :(得分:1)
尝试
with open('data.json', 'w') as f:
f.write(json.dumps(data))
with open('data.json', 'r') as f:
json.load(f)