如下图的“songs”中的"id": 1933232289
在json中出现了多次,想遍历输出应该如何操作,
"songs": [
{
"name": "slow ride",
"id": 1933232289,
"pst": 0,
"t": 0,
"ar": [
{
"id": 12919519,
"name": "Matt吕彦良",
"tns": [],
"alias": []
},
{
"id": 10473,
"name": "袁娅维TIA RAY",
"tns": [],
"alias": []
}
],
},
{
"name": "是我",
"id": 1432632134,
"pst": 0,
"t": 0,
"ar": [
{
"id": 33913095,
"name": "KIND",
"tns": [],
"alias": []
}
],
},
import json
json_response = """
{"songs" : [
{
"name": "slow ride",
"id": 1933232289,
"pst": 0,
"t": 0,
"ar": [
{
"id": 12919519,
"name": "Matt吕彦良",
"tns": [],
"alias": []
},
{
"id": 10473,
"name": "袁娅维TIA RAY",
"tns": [],
"alias": []
}
]
},
{
"name": "是我",
"id": 1432632134,
"pst": 0,
"t": 0,
"ar": [
{
"id": 33913095,
"name": "KIND",
"tns": [],
"alias": []
}
]
}
]}
"""
dict_json = json.loads(json_response)
ids = []
def deep(dct):
if type(dct) == dict:
for k, v in dct.items():
if type(v) == dict or type(v) == list:
deep(v)
elif k == 'id':
ids.append(v)
elif type(dct) == list:
for item in dct:
deep(item)
deep(dict_json)
for item in ids:
print(item)