如何检查API中是否存在python中的特定JSON值?
我不确定是否可以检查API中是否存在特定键的值获取请求响应。 以下代码不起作用。
req = requests.get('MYAPI')
response = req.json()
if 'Tom' in res:
print('Welcome')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
req.json
实际返回a 打印函数如下:因此,每当您想查找字典中的内容时,请使用
print
。此外,您还可以使用以下事实:字典也是一个可具的对象。这基本上意味着您可以按项目迭代字典项目。这是一个很好的答案 显示了如何。将其结合在一起,即您想找出其中至少其中一个项目是
“ Tom”
。你现在能弄清楚吗?req.json
actually returns a dictionary of items. To reveal the contents of a dictionary, you can useprint
function like below:So whenever you want to find what is inside a dictionary, use
print
.Additionally, you could also use the fact that a dictionary is also an iterable object. This basically means, that you can iterate through the dictionary item by item. Here's a good answer that shows you how. Combine that with the fact that you want to find out whether at least one of those items is
"Tom"
. Can you figure it out now?