Python;myLink=data[“items”][0][“link”]做什么?

2024-10-06 12:08:49 发布

您现在位置:Python中文网/ 问答频道 /正文

myLink=data[“items”][0][“link”]在下面的代码段中做什么?我明白它的意思;如果我的json数据结构有键“items”,那么[0][“link”]部分做什么呢

response = requests.get(resUrl)
data = response.json()

if data.has_key("items"):
    myLink = data["items"][0]["link"]
else:
    myLink = "no link found"

Tags: keyjson数据结构datagetifresponse代码段
1条回答
网友
1楼 · 发布于 2024-10-06 12:08:49
data = {'items':[{'link':'xxx'}, {'link':'yyy'}, {'link':'zzz'}]}

上面的dict()对象有一个dict()对象的内部数组作为值或键'items'.

为了获得这个值,data["items"]工作,而为了解析数组中的元素,[0]['link']工作

["items"] get the value of 'items' key # O/P [{'link':'xxx'}, {'link':'yyy'}, {'link':'zzz'}]
["items"][0] first index of the array # O/P {'link':'xxx'}
["items"][0]['link'] key of the first index #O/P 'xxx'

我要感谢你的勇气,你提出了一个基本的问题,许多人迟疑问和不及格,在不久的将来

干杯

相关问题 更多 >