可以使用 Python 自带的 json 模块将两个 JSON 文件加载为字典,然后进行数据的对比。
示例代码:
import json
def compare_json(file1, file2):
with open(file1, 'r') as f1:
dict1 = json.load(f1)
with open(file2, 'r') as f2:
dict2 = json.load(f2)
return dict1 == dict2
if __name__ == '__main__':
file1 = 'file1.json'
file2 = 'file2.json'
result = compare_json(file1, file2)
print(result)
其中,file1.json 和 file2.json 是两个需要进行比较的 JSON 文件。函数 compare_json 通过打开文件,使用 json.load 将其转换为字典,并返回两个字典的比对结果。最后,将比对结果打印输出。
下一篇:比较两个JSON文件