How does Python judge whether there is a key in the json data

Use the in operator to determine whether the key exists in the dictionary, and return true if the key is in the json dictionary, otherwise return false

test-json

test_json = {
    "name":"张三",
    "sex":"男"
}

print(test_json)
print(test_json["name"])
if "age" in test_json:
    print(test_json["age"])
else:
    print("test_json 不存在键“age”")

output result

 

Guess you like

Origin blog.csdn.net/luobowangjing/article/details/131983916