Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何获得所有的;ids";在python数组中_Python_Arrays_Json - Fatal编程技术网

如何获得所有的;ids";在python数组中

如何获得所有的;ids";在python数组中,python,arrays,json,Python,Arrays,Json,因此,我使用API提取一些json数据,它最初看起来是这样的: { "result": { "elements": [ { "id": "SV_3s0FmbrNancSmsB", "name": "Test Survey", "ownerId": "sdfsdfasdf", "lastModified": "2016-08-09T21:33:27Z", "isActive": false }, { "id": "SV_dgJOVyJv

因此,我使用API提取一些json数据,它最初看起来是这样的:

{
 "result": {
"elements": [
  {
    "id": "SV_3s0FmbrNancSmsB",
    "name": "Test Survey",
    "ownerId": "sdfsdfasdf",
    "lastModified": "2016-08-09T21:33:27Z",
    "isActive": false
  },
  {
    "id": "SV_dgJOVyJvwZR0593",
    "name": "Test Survey",
    "ownerId": "sdfdsfsdfs",
    "lastModified": "2016-08-04T17:53:37Z",
    "isActive": true
  }
],
"nextPage": null
  },
 "meta": {
 "httpStatus": "200 - OK"
}
}
因此,我想使用Python提取此JSON中的所有ID,下面是我的代码:

url = "random.com"

headers = {
'x-api-token': "dsfsdagdfa"
}

response = requests.get(url, headers=headers)

data = json.loads(response.text)

id_0 = data['result']['elements'][0]['id']

print(id_0)

这基本上只是打印所创建数组中的第一个Id。我该怎么做才能得到所有的ID?

这应该可以。我相信有一种更优雅的方式来理解地图或列表

ids = []

for elem in data['result']['elements']:
  ids.append(elem['id'])
如果你想懒洋洋地评估它,做一个生成器


ids=(数据中元素['result']['elements']的元素['id'])

您可以使用以下一行代码:

ids = [element['id'] for element in data['result']['elements']]
那么:

ids = [element['id'] for element in data['result']['elements']]

非常感谢。这是可行的,但是其他的更干净一些!是的,他们当然是!可能重复的