36 lines
1.2 KiB
Python
36 lines
1.2 KiB
Python
import json
|
|
|
|
def remove_refs_from_dict(data):
|
|
if isinstance(data, dict):
|
|
# Create a copy of keys to iterate over, as we might modify the dict
|
|
keys = list(data.keys())
|
|
for key in keys:
|
|
if key == "$ref" or key == "$$ref":
|
|
del data[key]
|
|
else:
|
|
remove_refs_from_dict(data[key])
|
|
elif isinstance(data, list):
|
|
for item in data:
|
|
remove_refs_from_dict(item)
|
|
return data
|
|
|
|
input_file = "doc/井筒API示例swagger_fixed_simple.json"
|
|
output_file = "doc/井筒API示例swagger_fixed_simple_noref_processed.json" # 输出到新文件
|
|
|
|
try:
|
|
with open(input_file, 'r', encoding='utf-8') as f:
|
|
json_data = json.load(f)
|
|
|
|
processed_data = remove_refs_from_dict(json_data)
|
|
|
|
with open(output_file, 'w', encoding='utf-8') as f:
|
|
json.dump(processed_data, f, ensure_ascii=False, indent=2)
|
|
|
|
print(f"成功处理文件,移除了 '$ref' 和 '$$ref' 字段。结果已保存到: {output_file}")
|
|
|
|
except FileNotFoundError:
|
|
print(f"错误: 输入文件未找到: {input_file}")
|
|
except json.JSONDecodeError:
|
|
print(f"错误: 输入文件 '{input_file}' 不是有效的 JSON 格式。")
|
|
except Exception as e:
|
|
print(f"处理过程中发生错误: {e}") |