小编Zac*_*ong的帖子

如何使用 Python 在 Json 数组中搜索特定值

我正在使用网站API来获取信息。它输出 100 个值。

在此数组中搜索特定值的最佳做法是什么?

数组看起来像这样:

{
  "success":true,
  "data":
  {
    "array":
    [
      {
        "id":"1","name":"Value1"
      },
      {
        "id":"2","name":"Value2"
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

我尝试使用我在别处找到的这个片段来搜索数据,但它不起作用:

for name in (r.json()['data']['array'][0]['name']):
    if r.json()['data']['array'][0] == s_name:
    ident = r.json()['data']['array'][0]['id']
    name = r.json()['data']['array'][0]['name']
    print (name + ' - ' + ident)
else:
    print ('Nothing was found.')
Run Code Online (Sandbox Code Playgroud)

因此,如果我试图参考Value1我将如何使用Python?

请注意:我是一个菜鸟开发者,第一次使用Json,对Python.

非常感谢所有帮助。

python json

7
推荐指数
3
解决办法
2万
查看次数

标签 统计

json ×1

python ×1