如何使用python访问json文件中的元素

时间:2017-01-10 14:03:15

标签: python json


我是python和json的新手,所以我有很多次问题确定spotify json文件的正确结构以便访问特定的项目。
好吧,使用python我开始处理json文件,所以我尝试使用python来解决这个问题 我总是要尝试不同的路径来访问,例如一个曲目名称,直到我来到右边,即“反复试验”方法。

我想要的是事先清楚地了解json结构所以我确切知道要使用哪条路径来访问特定元素。

以下是一个示例文件:

    {
  "href" : "https://api.spotify.com/v1/users/<user>/playlists/0WakN.../tracks?offset=0&limit=100&market=ES",
  "items" : [ {
    "added_at" : "2016-10-01T20:30:03Z",
    "added_by" : {
      "external_urls" : {
        "spotify" : "http://open.spotify.com/user/<user>"
      },
      "href" : "https://api.spotify.com/v1/users/<user>",
      "id" : "<user>",
      "type" : "user",
      "uri" : "spotify:user:<user>"
    },
    "is_local" : false,
    "track" : {
      "album" : {
        "album_type" : "album",
        "external_urls" : {
          "spotify" : "https://open.spotify.com/album/61Jlgdh66274FjDeqSosCU"
        },
        "href" : "https://api.spotify.com/v1/albums/61Jlgdh66274FjDeqSosCU",
        "id" : "61Jlgdh66274FjDeqSosCU",
        "images" : [ {
          "height" : 640,
          "url" : "https://i.scdn.co/image/8da566952192f2c07b0557ce0b18f730c84ba1a3",
          "width" : 640
        }, {
          "height" : 300,
          "url" : "https://i.scdn.co/image/904046908fb220aacf4308cd8ae055cb88d6a6af",
          "width" : 300
        }, {
          "height" : 64,
          "url" : "https://i.scdn.co/image/a08529c336b33937f0cbb678ffbb15e73f7f15fa",
          "width" : 64
        } ],
        "name" : "20 Años La Historia (En Vivo)",
        "type" : "album",
        "uri" : "spotify:album:61Jlgdh66274FjDeqSosCU"
      },
      "artists" : [ {
        "external_urls" : {
          "spotify" : "https://open.spotify.com/artist/2SYIF2tMAgLcjtTTeCfjg3"
        },
        "href" : "https://api.spotify.com/v1/artists/2SYIF2tMAgLcjtTTeCfjg3",
        "id" : "2SYIF2tMAgLcjtTTeCfjg3",
        "name" : "Porfi Baloa",
        "type" : "artist",
        "uri" : "spotify:artist:2SYIF2tMAgLcjtTTeCfjg3"
      } ],
      "disc_number" : 1,
      "duration_ms" : 207880,
      "explicit" : false,
      "external_ids" : {
        "isrc" : "QMHUY1559454"
      },
      "external_urls" : {
        "spotify" : "https://open.spotify.com/track/5VVIsLFq7c6CT0vsr9J3Py"
      },
      "href" : "https://api.spotify.com/v1/tracks/5VVIsLFq7c6CT0vsr9J3Py",
      "id" : "5VVIsLFq7c6CT0vsr9J3Py",
      "is_playable" : true,
      "linked_from" : {
        "external_urls" : {
          "spotify" : "https://open.spotify.com/track/5Cv09ZTlg3La9nlJejGlX7"
        },
        "href" : "https://api.spotify.com/v1/tracks/5Cv09ZTlg3La9nlJejGlX7",
        "id" : "5Cv09ZTlg3La9nlJejGlX7",
        "type" : "track",
        "uri" : "spotify:track:5Cv09ZTlg3La9nlJejGlX7"
      },
      "name" : "Dame una Oportunidad - En Vivo",
      "popularity" : 14,
      "preview_url" : "https://p.scdn.co/mp3-preview/8e3f576a0cf9aee6a0c52c2ae3b1c73bf9de2c7f",
      "track_number" : 2,
      "type" : "track",
      "uri" : "spotify:track:5VVIsLFq7c6CT0vsr9J3Py"
    }
  }, {
    "added_at" : "2016-10-04T20:03:20Z",
    "added_by" : {
      "external_urls" : {
        "spotify" : "http://open.spotify.com/user/<user>"
      },
      "href" : "https://api.spotify.com/v1/users/<user>",
      "id" : "<user>",
      "type" : "user",
      "uri" : "spotify:user:<user>"
    },
    "is_local" : false,
    "track" : {
      "album" : {
        "album_type" : "album",
        "external_urls" : {
          "spotify" : "https://open.spotify.com/album/0GxUdSpTj1PeJlqq0knC5I"
        },
        "href" : "https://api.spotify.com/v1/albums/0GxUdSpTj1PeJlqq0knC5I",
        "id" : "0GxUdSpTj1PeJlqq0knC5I",
        "images" : [ {
          "height" : 636,
          "url" : "https://i.scdn.co/image/ad15a75e71dce1a0c8e04f3d8a919a5185bcfa94",
          "width" : 640
        }, {
          "height" : 298,
          "url" : "https://i.scdn.co/image/9ceb0585e856a11002ca60388e1e8821316f5f2a",
          "width" : 300
        }, {
          "height" : 64,
          "url" : "https://i.scdn.co/image/08822c412704203f7fb80f9ba3bab3147180848f",
          "width" : 64
        } ],
        "name" : "Contraste En Salsa",
        "type" : "album",
        "uri" : "spotify:album:0GxUdSpTj1PeJlqq0knC5I"
      },
      "artists" : [ {
        "external_urls" : {
          "spotify" : "https://open.spotify.com/artist/27vNK840zYq6IfDijHPsv1"
        },
        "href" : "https://api.spotify.com/v1/artists/27vNK840zYq6IfDijHPsv1",
        "id" : "27vNK840zYq6IfDijHPsv1",
        "name" : "Gilberto Santa Rosa",
        "type" : "artist",
        "uri" : "spotify:artist:27vNK840zYq6IfDijHPsv1"
      } ],
      "disc_number" : 1,
      "duration_ms" : 266786,
      "explicit" : false,
      "external_ids" : {
        "isrc" : "USSD10704531"
      },
      "external_urls" : {
        "spotify" : "https://open.spotify.com/track/5YfqVqJwjxiyhxvofbCnHp"
      },
      "href" : "https://api.spotify.com/v1/tracks/5YfqVqJwjxiyhxvofbCnHp",
      "id" : "5YfqVqJwjxiyhxvofbCnHp",
      "is_playable" : true,
      "name" : "Conteo Regresivo - Salsa Version",
      "popularity" : 56,
      "preview_url" : "https://p.scdn.co/mp3-preview/cea9a58f7b87267974d8f8261eb4670be3129777",
      "track_number" : 1,
      "type" : "track",
      "uri" : "spotify:track:5YfqVqJwjxiyhxvofbCnHp"
    }
  }, {
    "added_at" : "2016-10-05T19:11:57Z",
    "added_by" : {
      "external_urls" : {
        "spotify" : "http://open.spotify.com/user/<user>"
      },
      "href" : "https://api.spotify.com/v1/users/<user>",
      "id" : "<user>",
      "type" : "user",
      "uri" : "spotify:user:<user>"
    },
    "is_local" : false,
    "track" : {
      "album" : {
        "album_type" : "album",
        "external_urls" : {
          "spotify" : "https://open.spotify.com/album/70XO7jxoVIV9p9IFyjXGvJ"
        },
        "href" : "https://api.spotify.com/v1/albums/70XO7jxoVIV9p9IFyjXGvJ",
        "id" : "70XO7jxoVIV9p9IFyjXGvJ",
        "images" : [ {
          "height" : 640,
          "url" : "https://i.scdn.co/image/5314594bebbeb59cb89b0779e03d9cdf0f84fe73",
          "width" : 640
        }, {
          "height" : 300,
          "url" : "https://i.scdn.co/image/888f3bcd4f7644655ec966b2c80bb0d591e5b17f",
          "width" : 300
        }, {
          "height" : 64,
          "url" : "https://i.scdn.co/image/7e9afbb36171a99f980ea7b71142a5e7fec68f47",
          "width" : 64
        } ],
        "name" : "Sigo Siendo Yo",
        "type" : "album",
        "uri" : "spotify:album:70XO7jxoVIV9p9IFyjXGvJ"
      },
      "artists" : [ {
        "external_urls" : {
          "spotify" : "https://open.spotify.com/artist/4wLXwxDeWQ8mtUIRPxGiD6"
        },
        "href" : "https://api.spotify.com/v1/artists/4wLXwxDeWQ8mtUIRPxGiD6",
        "id" : "4wLXwxDeWQ8mtUIRPxGiD6",
        "name" : "Marc Anthony",
        "type" : "artist",
        "uri" : "spotify:artist:4wLXwxDeWQ8mtUIRPxGiD6"
      } ],
      "disc_number" : 1,
      "duration_ms" : 289293,
      "explicit" : false,
      "external_ids" : {
        "isrc" : "NLB630400179"
      },
      "external_urls" : {
        "spotify" : "https://open.spotify.com/track/4pUmQqaZklcCOB3rFdWc7g"
      },
      "href" : "https://api.spotify.com/v1/tracks/4pUmQqaZklcCOB3rFdWc7g",
      "id" : "4pUmQqaZklcCOB3rFdWc7g",
      "is_playable" : true,
      "name" : "Valió la Pena - Salsa Version",
      "popularity" : 65,
      "preview_url" : "https://p.scdn.co/mp3-preview/50e896ef946dc1585153b380a9f63d02f8244553",
      "track_number" : 9,
      "type" : "track",
      "uri" : "spotify:track:4pUmQqaZklcCOB3rFdWc7g"
    }
  } ],
  "limit" : 100,
  "next" : null,
  "offset" : 0,
  "previous" : null,
  "total" : 3
}

访问例如我使用的播放列表中的曲目的艺术家名称:

data = json.load(playlist_file)
artist_name = data["items"][x]["track"]["artists"][0]["name"]

但我“失去”了很长一段时间才走上这条道路。 是否有一些方法可以清楚地看到长json 文件的结构?

我认为可能只是打印所有括号的键可以使整体更清晰(因为许多值很长)?

由于

1 个答案:

答案 0 :(得分:0)

如果您的目标是漂亮印刷,

f = open('/path/to/spotify.json', 'r')
loaded = json.load(f)
print json.dumps(loaded, indent=4)

<强>更新

获取json中的所有可能路径

import ijson

f = open('spotify.json', 'r')
parser = ijson.parse(f)
paths = sorted(set(prefix for prefix, event, value in parser if prefix))

for path in paths:
    print path

样本结果:

    href
    items
    items.item
    items.item.added_at
    items.item.added_by
    items.item.added_by.external_urls
    items.item.added_by.external_urls.spotify
    items.item.added_by.href
    items.item.added_by.id
    ....

此处 item 表示存在数组