1 回答

TA贡献1864条经验 获得超6个赞
我认为这会做你想做的。在json_obj_list返回的形式json.load()实际上是一个Python字典,所以你需要重复的值json_obj_list['data']。为了使代码对现有变量名称保持合理,我将其修改为直接从返回的字典中检索 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)
添加回答
举报