【问题标题】:extract all Json key values提取所有 Json 键值
【发布时间】:2021-06-04 09:55:08
【问题描述】:

我不熟悉 Python Json。我有这些 Json 结果:

{
    "href": "https://api.spotify.com/v1/users/wizzler/playlists",
    "items": [
        {
            "collaborative": false,
            "external_urls": {
                "spotify": "http://open.spotify.com/user/wizzler/playlists/53Y8wT46QIMz5H4WQ8O22c"
            },
            "href": "https://api.spotify.com/v1/users/wizzler/playlists/53Y8wT46QIMz5H4WQ8O22c",
            "id": "53Y8wT46QIMz5H4WQ8O22c",
            "images": [],
            "name": "Wizzlers Big Playlist",
            "owner": {
                "external_urls": {
                    "spotify": "http://open.spotify.com/user/wizzler"
                },
                "href": "https://api.spotify.com/v1/users/wizzler",
                "id": "wizzler",
                "type": "user",
                "uri": "spotify:user:wizzler"
            },
            "public": true,
            "snapshot_id": "bNLWdmhh+HDsbHzhckXeDC0uyKyg4FjPI/KEsKjAE526usnz2LxwgyBoMShVL+z+",
            "tracks": {
                "href": "https://api.spotify.com/v1/users/wizzler/playlists/53Y8wT46QIMz5H4WQ8O22c/tracks",
                "total": 30
            },
            "type": "playlist",
            "uri": "spotify:user:wizzler:playlist:53Y8wT46QIMz5H4WQ8O22c"
        },
        {
            "collaborative": false,
            "external_urls": {
                "spotify": "http://open.spotify.com/user/wizzlersmate/playlists/1AVZz0mBuGbCEoNRQdYQju"
            },
            "href": "https://api.spotify.com/v1/users/wizzlersmate/playlists/1AVZz0mBuGbCEoNRQdYQju",
            "id": "1AVZz0mBuGbCEoNRQdYQju",
            "images": [],
            "name": "Another Playlist",
            "owner": {
                "external_urls": {
                    "spotify": "http://open.spotify.com/user/wizzlersmate"
                },
                "href": "https://api.spotify.com/v1/users/wizzlersmate",
                "id": "wizzlersmate",
                "type": "user",
                "uri": "spotify:user:wizzlersmate"
            },
            "public": true,
            "snapshot_id": "Y0qg/IT5T02DKpw4uQKc/9RUrqQJ07hbTKyEeDRPOo9LU0g0icBrIXwVkHfQZ/aD",
            "tracks": {
                "href": "https://api.spotify.com/v1/users/wizzlersmate/playlists/1AVZz0mBuGbCEoNRQdYQju/tracks",
                "total": 58
            },
            "type": "playlist",
            "uri": "spotify:user:wizzlersmate:playlist:1AVZz0mBuGbCEoNRQdYQju"
        }
    ],
    "limit": 9,
    "next": null,
    "offset": 0,
    "previous": null,
    "total": 9
}

现在我只需要提取播放列表 ID。该怎么做?

编辑:

我通过以下方式获得 Json 数据:

r = requests.get(BASE_URL + 'users/' + user_id + '/playlists', headers=headers)

r = r.json()

print(r) 将 Json 数据返回给我。当我尝试data = json.load(r) 我得到这些错误! AttributeError: 'dict' object has no attribute 'read'

【问题讨论】:

  • playlist_ids = [item.get('id') for item in data.get('items')],是 JSON 对象的“数据”。

标签: python json


【解决方案1】:

首先,使用内置的 json 库加载 JSON 文件。

import json
with open('path/to/json/file.json') as f:
    data = json.load(f)

然后,使用列表推导仅获取 ID。

playlist_ids = [item['id'] for item in data['items']]

编辑: 或者,如果您已经解析了 JSON,只需使用列表推导即可。不要这样做r = r.json(),这会将请求对象重置为数据。将其设置为其他变量,data 可以 - data = r.json()

playlist_ids = [item['id'] for item in data['items']]

编辑 2:如果您只想要所有者 ID 为 "wizzler" 的位置,则将 if 子句添加到列表推导中。

playlist_ids = [item['id'] for item in data['items'] if item['owner']['id'] == 'wizzler']

【讨论】:

  • 我不能做 data = json.load(r) 请看我的编辑
  • 嘿。编辑了我的答案。
  • 我以为你发了一条评论,问我是否有办法只过滤所有者 ID 为"wizzler" 的地方,现在似乎被删除了?如果您不需要该功能,请告诉我,我会回滚我的编辑。
猜你喜欢
  • 1970-01-01
  • 2023-01-22
  • 1970-01-01
  • 2014-07-31
  • 1970-01-01
  • 2021-02-21
  • 2020-05-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多