| import json | |
| # 加载JSON数据的工具函数 | |
| def load_json(file_path): | |
| with open(file_path, 'r', encoding='utf-8') as f: | |
| return json.load(f) | |
| # 保存JSON数据 | |
| def save_json(data, file_path): | |
| with open(file_path, 'w', encoding='utf-8') as f: | |
| json.dump(data, f, indent=2, ensure_ascii=False) | |
| # 主逻辑 | |
| def add_stereo_audio_field(array_json_path, dict_json_path, output_path): | |
| array_data = load_json(array_json_path) # 加载JSON数组 | |
| dict_data = load_json(dict_json_path) # 加载JSON字典 | |
| # 遍历数组中的每一项,根据"key"从字典中查找对应的"stereo_audio" | |
| for item in array_data: | |
| key = item.get("key") | |
| if key in dict_data: | |
| stereo_audio = dict_data[key].get("stereo_audio") | |
| item["audio_url"] = stereo_audio # 添加字段 | |
| # 保存结果 | |
| save_json(array_data, output_path) | |
| # 示例调用 | |
| if __name__ == "__main__": | |
| add_stereo_audio_field( | |
| array_json_path="./test.json", | |
| dict_json_path="./overlaps.json", | |
| output_path="test.json" | |
| ) | |