提前感谢您提供的任何帮助。
我正在使用Django Rest Framework和Vue.js构建应用程序。我正在使用Spotify API以及从DRF提供数据。当我使用以下代码进行艺术家搜索时:
np.array([(755, 855, 755, 855, 743, 843, 743, 843, 2),
(755, 855, 755, 855, 743, 843, 743, 843, 2),
(755, 855, 755, 855, 743, 843, 743, 843, 2),
(693, 793, 693, 793, 693, 793, 693, 793, 1),
(693, 793, 693, 793, 693, 793, 693, 793, 1),
(755, 855, 755, 855, 743, 843, 743, 843, 2),
(693, 793, 693, 793, 693, 793, 693, 793, 1)],
dtype=[('sensorA', '<u4'), ('sensorB', '<u4'),
('sensorC', '<u4'), ('sensorD', '<u4'),
('sensorE', '<u4'), ('sensorF', '<u4'),
('sensorG', '<u4'), ('sensorH', '<u4'),
('signal', '<u4')])
它完全返回我所期望的Spotify API的艺术家搜索。结果看起来像这样。(我只是为了简洁而显示前两个条目)
def get(self, request, *args, **kwargs):
artist = services.spot_artist_search(self.kwargs['artist'])
return Response(artist)
我想要做的是遍历此响应,并提取“&#39;图像”中的第一个条目。嵌套列表,然后用只有那个第一个条目替换嵌套列表。最终的JSON看起来像这样:
{
"artists": {
"href": "https://api.spotify.com/v1/search?query=adele&type=artist&offset=0&limit=10",
"items": [
{
"external_urls": {
"spotify": "https://open.spotify.com/artist/4dpARuHxo51G3z768sgnrY"
},
"followers": {
"href": null,
"total": 6175115
},
"genres": [
"dance pop",
"pop"
],
"href": "https://api.spotify.com/v1/artists/4dpARuHxo51G3z768sgnrY",
"id": "4dpARuHxo51G3z768sgnrY",
"images": [
{
"height": 1000,
"url": "https://i.scdn.co/image/ccbe7b4fef679f821988c78dbd4734471834e3d9",
"width": 1000
},
{
"height": 640,
"url": "https://i.scdn.co/image/f8737f6fda048b45efe91f81c2bda2b601ae689c",
"width": 640
},
{
"height": 200,
"url": "https://i.scdn.co/image/df070ad127f62d682596e515ac69d5bef56e0897",
"width": 200
},
{
"height": 64,
"url": "https://i.scdn.co/image/cbbdfb209cc38b2999b1882f42ee642555316313",
"width": 64
}
],
"name": "Adele",
"popularity": 85,
"type": "artist",
"uri": "spotify:artist:4dpARuHxo51G3z768sgnrY"
},
{
"external_urls": {
"spotify": "https://open.spotify.com/artist/19RHMn8FFkEFmhPwyDW2ZC"
},
"followers": {
"href": null,
"total": 3504
},
"genres": [],
"href": "https://api.spotify.com/v1/artists/19RHMn8FFkEFmhPwyDW2ZC",
"id": "19RHMn8FFkEFmhPwyDW2ZC",
"images": [],
"name": "Robyn Adele Anderson",
"popularity": 42,
"type": "artist",
"uri": "spotify:artist:19RHMn8FFkEFmhPwyDW2ZC"
},
{
"external_urls": {
"spotify": "https://open.spotify.com/artist/5yUp79jSBSGdkbufl2hmcY"
},
"followers": {
"href": null,
"total": 477
},
"genres": [
"violin"
],
"href": "https://api.spotify.com/v1/artists/5yUp79jSBSGdkbufl2hmcY",
"id": "5yUp79jSBSGdkbufl2hmcY",
"images": [
{
"height": 636,
"url": "https://i.scdn.co/image/581e634f260b77c93c61266bc6f8f755c2417a67",
"width": 640
},
{
"height": 298,
"url": "https://i.scdn.co/image/01aa0f1316ac6cc2f03306aaf851f32b20f50175",
"width": 300
},
{
"height": 64,
"url": "https://i.scdn.co/image/2051799990aa97da74524fdf9757e499cc51569b",
"width": 64
}
],
"name": "Adele Anthony",
"popularity": 34,
"type": "artist",
"uri": "spotify:artist:5yUp79jSBSGdkbufl2hmcY"
},
请你能告诉我怎么做这个?我还需要检查嵌套列表中是否有图像,因为并非所有结果都会返回一个图像。
答案 0 :(得分:1)
好的,请尝试使用以下
# result = <api call>
for i in result["artists"]["items"]:
if "images" in i and i["images"]:
i["images"] = [i["images"][0]]
print result
看看这是否有效。