1 回答

TA貢獻(xiàn)1864條經(jīng)驗 獲得超6個贊
我認(rèn)為這會做你想做的。在json_obj_list返回的形式j(luò)son.load()實際上是一個Python字典,所以你需要重復(fù)的值json_obj_list['data']。為了使代碼對現(xiàn)有變量名稱保持合理,我將其修改為直接從返回的字典中檢索 JSON 對象列表,json.load()如下所示:
json_obj_list = json.load(in_json_file)['data']
這是完整的代碼:
import json
in_file_path = 'testfile.json'
with open(in_file_path,'r') as in_json_file:
# Read the file and get the list from the dictionary.
json_obj_list = json.load(in_json_file)['data']
for json_obj in json_obj_list:
filename = json_obj['number']+'.json' # Changed this, too, per comment by OP.
print('creating file:', filename)
with open(filename, 'w') as out_json_file:
# Save each obj to their respective filepath
# with pretty formatting thanks to `indent=4`
json.dump(json_obj, out_json_file, indent=4)
添加回答
舉報