如何检查API中是否存在python中的特定JSON值?

发布于 2025-02-10 18:10:58 字数 162 浏览 0 评论 0 原文

我不确定是否可以检查API中是否存在特定键的值获取请求响应。 以下代码不起作用。

 req = requests.get('MYAPI')
 response = req.json()
 if 'Tom' in res:
   print('Welcome')

I am not sure if it's possible to check if a value of a specific key exists in an API GET Request Response.
The below code does not work.

 req = requests.get('MYAPI')
 response = req.json()
 if 'Tom' in res:
   print('Welcome')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骑趴 2025-02-17 18:10:58

req.json 实际返回a 打印函数如下:

my_dictionary = {'first_name': 'joe', 'last_name': 'biden', 'age': 14}
print(my_dictionary)  # {'first_name': 'joe', 'last_name': 'biden', 'age': 14}

因此,每当您想查找字典中的内容时,请使用 print

此外,您还可以使用以下事实:字典也是一个可具的对象。这基本上意味着您可以按项目迭代字典项目。这是一个很好的答案 显示了如何。将其结合在一起,即您想找出其中至少其中一个项目是“ Tom” 。你现在能弄清楚吗?

req.json actually returns a dictionary of items. To reveal the contents of a dictionary, you can use print function like below:

my_dictionary = {'first_name': 'joe', 'last_name': 'biden', 'age': 14}
print(my_dictionary)  # {'first_name': 'joe', 'last_name': 'biden', 'age': 14}

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文