可以使用 Python 中的 json 模块来处理 JSON 数据。比较两个JSON字符串中某个键的值是否相等,可以通过先将 JSON 字符串转为 Python 字典,然后直接比较相应键的值。
例如,比较两个 JSON 字符串中 "name" 键所对应的值是否相等:
import json
str1 = '{"name": "Alice", "age": 18}'
str2 = '{"name": "Bob", "age": 20}'
json1 = json.loads(str1)
json2 = json.loads(str2)
if json1['name'] == json2['name']:
print("The values of 'name' are the same.")
else:
print("The values of 'name' are different.")
输出结果:
The values of 'name' are different.