Python在列表

时间:2018-05-21 17:20:51

标签: python json python-3.x rest python-requests

我正在尝试在json中创建API查询结果并将值存储到列表中。

import requests
url = "https://api_url/json/"
headers = {
    'auth': "0dXKLHO",
    }
response = requests.request('GET', url, headers=headers, verify=False)
json_data = response.json()
details = []
for data in json_data["data"]["segment"]:
    output = (data['value'])
    details.append(output)
    print(details)

实际JSON是,

{
    "data": {
        "segments": [
            {
                "id": "1",
                "event_id": "1",
                "object_id": "0",
                "object_relation": null
                "value": "103"
            },
            {
                "id": "2",
                "event_id": "1",
                "object_id": "0",
                "object_relation": null
                "value": "104"
            },
            {
                "id": "3",
                "event_id": "1",
                "object_id": "0",
                "object_relation": null
                "value": "105"
            }
        ]
    }
}

我的代码给了我以下输出,

['103']
['103','104']
['103','104','105']

但是预期的输出是单列表,

['103','104','105']

0 个答案:

没有答案