Как получить доступ к элементу из объекта JSON - PullRequest
0 голосов
/ 23 июня 2019

Как я могу получить доступ к каждому элементу из объекта json, используя beautifulsoup или regex?

Например, вызвать временную метку и получить результат = 1561238146781

{
  "timestamp" : 1561238146781,
  "context" : {
    "external_urls" : {
      "spotify" : "https://open.spotify.com/playlist/4rwZyPeww8O7ZPnQqHD2q"
    },
    "href" : "https://api.spotify.com/v1/playlists/4rwZyPqew8O7ZPnQqHD2q",
    "type" : "playlist",
    "uri" : 
"spotify:user:3sui5bqpr8a0i2t:playlist:4rwZyPqewO7ZPnQqHD2q"
  },
  "progress_ms" : 115728,
  "item" : {
    "album" : {
      "album_type" : "album",
      "artists" : [ {
        "external_urls" : {
          "spotify" : 
"https://open.spotify.com/artist/5eAWCfyUhZqwHtBdNk56l1"
        },
        "href" : 
"https://api.spotify.com/v1/artists/5eAWCfyeZtHHtBdNk56l1",
        "id" : "5eAWCfyUhZtHHtBdNk56l1",
        "name" : "System Of A Down",
        "type" : "artist",
        "uri" : "spotify:artist:5eAWewyUhZtHHtBdNk56l1"
      } ],
      "available_markets" : [ ],
      "external_urls" : {
        "spotify" : "https://open.spotify.com/album/4DR0Gds7w2GJyQnFVa4jAB"
      },
      "href" : "https://api.spotify.com/v1/albums/4DR0ewwsdJyQnFVa4jAB",
      "id" : "4DR0GWo7w2ewyQnFVa4jAB",
      "images" : [ {
        "height" : 640,
        "url" : 
"https://i.scdn.co/image/932e185b217ew6caasd837dbe30d54028de9cfc",
        "width" : 615
      }, {
        "height" : 300,
        "url" : 
"https://i.scdn.co/image/30de1d4e1ew38sd89573893d8494fd6a66",
        "width" : 288
      }, {
        "height" : 64,
        "url" : 
"https://i.scdn.co/image/1682cd0e8ew8bf87sdc4cd1e01ce24cd165b",
        "width" : 62
      } ],
      "name" : "Toxicity",
      "release_date" : "2001-01-01",
      "release_date_precision" : "day",
      "total_tracks" : 14,
      "type" : "album",
      "uri" : "spotify:album:4DR0GewdGJyQnFVa4jAB"
    },
    "artists" : [ {
      "external_urls" : {
        "spotify" : "https://open.spotify.com/artist/5eAWCsdUweHtBdNk56l1"
      },
      "href" : "https://api.spotify.com/v1/artists/5eAWCfewhdsHtBdNk56l1",
      "id" : "5eAWCfyUhZtHHtBewk56l1",
      "name" : "System Of A Down",
      "type" : "artist",
      "uri" : "spotify:artist:5eAWCfyUsdtHHtBdNk56l1"
    } ],
    "available_markets" : [ ],
    "disc_number" : 1,
    "duration_ms" : 235599,
    "explicit" : false,
    "external_ids" : {
      "isrc" : "USSM10107264"
    },
    "external_urls" : {
      "spotify" : "https://open.spotify.com/track/1twBtsdaZiy7HWPG025QGuP"
    },
    "href" : "https://api.spotify.com/v1/tracks/1twBt7aZiy7HWPG025QGuP",
    "id" : "1twBt7aZiy7HWweG025QGuP",
    "is_local" : false,
    "name" : "Aerials",
    "popularity" : 9,
    "preview_url" : null,
    "track_number" : 14,
    "type" : "track",
    "uri" : "spotify:track:1twBt7aZieWPG025QGuP"
  },
  "currently_playing_type" : "track",
  "actions" : {
    "disallows" : {
      "resuming" : true
    }
  },
  "is_playing" : true
}

Позвони "is-Playing" и получи правду

Я пытался сделать массив отверстий списком (каждый элемент) и пытался получить элемент из списка, но понял, что это вряд ли полезно.

1 Ответ

1 голос
/ 23 июня 2019

превратите ваши данные json в файл data.json, затем выполните этот код:

import json

with open('data.json',"r") as f:
    data = json.load(f)
print(data["is_playing"])

вывод

True
...