我从请求调用返回一个JSON对象。我想从中获取所有值并将它们存储在一个平面阵列中。
我的JSON对象:
[
{
"link": "https://f.com/1"
},
{
"link": "https://f.com/2"
},
{
"link": "https://f.com/3"
}
]
我想将其存储为:
[https://f.com/things/1, https://f.com/things/2, https://f.com/things/3]
我的代码如下..它只是打印出每个链接:
import requests
import json
def start_urls_data():
url = 'http://106309.n.com:3000/api/v1/product_urls?q%5Bcompany_in%5D%5B%5D=F'
headers = {'X-Api-Key': '1', 'Content-Type': 'application/json'}
r = requests.get(url, headers=headers)
start_urls_data = json.loads(r.content)
for i in start_urls_data:
print i['link']
答案 0 :(得分:2)
您可以使用简单的列表理解:
data = [
{
"link": "https://f.com/1"
},
{
"link": "https://f.com/2"
},
{
"link": "https://f.com/3"
}
]
print([x["link"] for x in data])
此代码循环遍历列表data
,并将密钥link
的值从dict元素放入新列表。