无论Python中的顺序如何,如何比较 JSON 对象?
JSON 是Java Script Object Notation。这些是用于数据交换的独立于语言的源代码,并且本质上通常是轻量级的。它充当 XML 的替代品。这些通常是人类可以轻松阅读和编写的文本,并且机器也更容易解析 JSON 并生成结果。 JSON 主要用于服务器和 Web 应用程序之间的数据传输。 .
在本文中,我们将学习如何比较 JSON 对象,而不管它们在Python中的存在顺序。
方法:
- 导入模块
- 创建 JSON字符串
- 将字符串转换为Python字典
- 排序字典
- 相比
- 打印结果
下面给出了执行相同操作的各种实现,
示例 1:使用sorted()
Python3
import json
# JSON string
json_1 = '{"Name":"GFG", "Class": "Website", "Domain":"CS/IT", "CEO":"Sandeep Jain"}'
json_2 = '{"CEO":"Sandeep Jain", "Domain":"CS/IT","Name": "GFG","Class": "Website"}'
# Converting string into Python dictionaries
json_dict1 = json.loads(json_1)
json_dict2 = json.loads(json_2)
print(sorted(json_dict1.items()) == sorted(json_dict2.items()))
Python3
import json
# JSON string
json_1 = '{"Name":"GFG", "Class": "Website", "Domain":"CS/IT", "CEO":"Sandeep Jain","Subjects":["DSA","Python","C++","Java"]}'
json_2 = '{"CEO":"Sandeep Jain","Subjects":["C++","Python","DSA","Java"], "Domain":"CS/IT","Name": "GFG","Class": "Website"}'
# Convert string into Python dictionary
json1_dict = json.loads(json_1)
json2_dict = json.loads(json_2)
print(sorted(json1_dict.items()) == sorted(json2_dict.items()))
print(sorted(json1_dict.items()))
print(sorted(json2_dict.items()))
Python3
import json
# JSON string
json_1 = '{"Name":"GFG", "Class": "Website", "Domain":"CS/IT", "CEO":"Sandeep Jain","Subjects":["DSA","Python","C++","Java"]}'
json_2 = '{"CEO":"Sandeep Jain","Subjects":["C++","Python","DSA","Java"], "Domain":"CS/IT","Name": "GFG","Class": "Website"}'
# Convert string into Python dictionary
json1_dict = json.loads(json_1)
json2_dict = json.loads(json_2)
def sorting(item):
if isinstance(item, dict):
return sorted((key, sorting(values)) for key, values in item.items())
if isinstance(item, list):
return sorted(sorting(x) for x in item)
else:
return item
print(sorting(json1_dict) == sorting(json2_dict))
输出:
True
示例 2:更复杂的比较
蟒蛇3
import json
# JSON string
json_1 = '{"Name":"GFG", "Class": "Website", "Domain":"CS/IT", "CEO":"Sandeep Jain","Subjects":["DSA","Python","C++","Java"]}'
json_2 = '{"CEO":"Sandeep Jain","Subjects":["C++","Python","DSA","Java"], "Domain":"CS/IT","Name": "GFG","Class": "Website"}'
# Convert string into Python dictionary
json1_dict = json.loads(json_1)
json2_dict = json.loads(json_2)
print(sorted(json1_dict.items()) == sorted(json2_dict.items()))
print(sorted(json1_dict.items()))
print(sorted(json2_dict.items()))
输出:
False
[(‘CEO’, ‘Sandeep Jain’), (‘Class’, ‘Website’), (‘Domain’, ‘CS/IT’), (‘Name’, ‘GFG’), (‘Subjects’, [‘DSA’, ‘Python’, ‘C++’, ‘Java’])]
[(‘CEO’, ‘Sandeep Jain’), (‘Class’, ‘Website’), (‘Domain’, ‘CS/IT’), (‘Name’, ‘GFG’), (‘Subjects’, [‘C++’, ‘Python’, ‘DSA’, ‘Java’])]
在这种情况下,我们得到的结果为 False,因为 sorted() 方法的问题是它只适用于字典的顶层,即键而不是它们的值,正如上面的代码可以验证的那样。因此,在这种情况下,我们可以自己定义一个自定义函数,它可以对任何列表或字典进行递归排序(通过将字典转换为键值对列表),从而使它们适合进行比较。下面给出了使用这种替代方法的实现。
例子:
蟒蛇3
import json
# JSON string
json_1 = '{"Name":"GFG", "Class": "Website", "Domain":"CS/IT", "CEO":"Sandeep Jain","Subjects":["DSA","Python","C++","Java"]}'
json_2 = '{"CEO":"Sandeep Jain","Subjects":["C++","Python","DSA","Java"], "Domain":"CS/IT","Name": "GFG","Class": "Website"}'
# Convert string into Python dictionary
json1_dict = json.loads(json_1)
json2_dict = json.loads(json_2)
def sorting(item):
if isinstance(item, dict):
return sorted((key, sorting(values)) for key, values in item.items())
if isinstance(item, list):
return sorted(sorting(x) for x in item)
else:
return item
print(sorting(json1_dict) == sorting(json2_dict))
输出:
True