0
あなたが提供できる援助を前もってありがとうございます。Djangoでネストされたリストを抽出し、値を置換する
私はDjango Rest FrameworkとVue.jsを使用してアプリケーションを構築しています。私はSpotify APIを消費しているだけでなく、DRFからデータを提供しています。
def get(self, request, *args, **kwargs):
artist = services.spot_artist_search(self.kwargs['artist'])
return Response(artist)
私はSpotifyのAPIからアーティスト検索を正確に返します。結果は次のようになります。(私は簡潔にするために最初の2つのエントリを示しています)私はこの応答を繰り返し処理されませんし、「画像」、ネストされたリストの最初のエントリを抽出したいと思い何
{
"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"
},
ネストされたリストを最初の1つのエントリだけに置き換えます。最後の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
},
],
"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
},
],
"name": "Adele Anthony",
"popularity": 34,
"type": "artist",
"uri": "spotify:artist:5yUp79jSBSGdkbufl2hmcY"
},
どうすればよいでしょうか?また、すべての結果が1を返すわけではないので、ネストされたリストに画像があるかどうかを確認する必要があります。
ありがとうございます。私はちょうどそれを試み、私は 'dict'オブジェクトを取得している属性 'has_key'エラーがありません。 –
[OK]を更新しました。 – user6731765