gpt4 book ai didi

python - 如何解析这个 JSON 字典以打印出键值对?

转载 作者:行者123 更新时间:2023-12-02 03:56:42 25 4
gpt4 key购买 nike

cow = {
"data": [
{
"id": "1179640025492630",
"indicator": "google-analytics|UA-97996598-1",
"type": "API_KEY"
},
{
"id": "1469013519830038",
"indicator": "google-analytics|UA-96613605-2",
"type": "API_KEY"
},
{
"id": "1459958767410551",
"indicator": "google-analytics|UA-86399386-2",
"type": "API_KEY"
},
{
"id": "1507839102569000",
"indicator": "google-analytics|UA-89570367-2",
"type": "API_KEY"
},
{
"id": "1276736575767341",
"indicator": "google-analytics|UA-69774312-4",
"type": "API_KEY"
},
{
"id": "1292251910882451",
"indicator": "google-analytics|UA-93952538-3",
"type": "API_KEY"
}
],
"paging": {
"cursors": {
"after": "NQZDZD",
"before": "MAZDZD"
}
}
}

我想从数据字典中提取“id”:“1179640025492630”。

我尝试使用 for 循环,例如:

for i in cow['data']:
for key,value in i:
if(i == 'id'):
print key,value
else:
pass

它给了我一个值错误,例如:

Traceback (most recent call last):
File "./dict.py", line 14, in <module>
for key,value in ['data']:
ValueError: too many values to unpack

如果我使用.iteritems,我会得到一个AttributeError:

Traceback (most recent call last):
File "./dict.py", line 14, in <module>
for key,value in ['data'].iteritems:
AttributeError: 'list' object has no attribute 'iteritems'

最佳答案

如果您只想打印与 id 关联的所有值:

for thing in cow["data"]:
print(thing["id"])

输出:

1179640025492630
1469013519830038
1459958767410551
1507839102569000
1276736575767341
1292251910882451

关于python - 如何解析这个 JSON 字典以打印出键值对?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43660303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com