我需要检查JSON请求是否指定了字段。我的请求可以是:
{"ip": "8.35.60.229", "blackListCountry" : "Az"}
或者干脆:{"ip": "8.35.60.229"}
。你知道吗
如何检查其中是否存在blackListCountry
?你知道吗
userIP = request.json["ip"]
blackListCountry = request.json["blackListCountry"]
print(blackListCountry)
Tags:
request.json()
实际返回dict,因此可以使用.get()
方法,如果找不到键,则返回None
:最简单的方法:
in
搜索“blackListCountry”键并返回bool True或False。你知道吗相关问题 更多 >
编程相关推荐