首页 > 解决方案 > 如何从 github gist API 获取公共 gist 文件名

问题描述

我正在尝试从 github gist API 获取文件名,这是我在调用 gist api 时得到的以下输出。但问题是我如何从输出中获取文件名。因为文件名在文件下,我想显示文件名。那我该怎么做。我是 API 新手,所以只能显示 url、id 等。

 {
        "url": "https://api.github.com/gists/41da54b03986886e1a4e57c8cdb38dc2",
        "forks_url": "https://api.github.com/gists/41da54b03986886e1a4e57c8cdb38dc2/forks",
        "commits_url": "https://api.github.com/gists/41da54b03986886e1a4e57c8cdb38dc2/commits",
        "id": "41da54b03986886e1a4e57c8cdb38dc2",
        "node_id": "MDQ6R2lzdDQxZGE1NGIwMzk4Njg4NmUxYTRlNTdjOGNkYjM4ZGMy",
        "git_pull_url": "https://gist.github.com/41da54b03986886e1a4e57c8cdb38dc2.git",
        "git_push_url": "https://gist.github.com/41da54b03986886e1a4e57c8cdb38dc2.git",
        "html_url": "https://gist.github.com/41da54b03986886e1a4e57c8cdb38dc2",
        "files": {
          "telegram-white.svg": {
            "filename": "telegram-white.svg",
            "type": "image/svg+xml",
            "language": "SVG",
            "raw_url": "https://gist.githubusercontent.com/cmschandan/41da54b03986886e1a4e57c8cdb38dc2/raw/16b1aac41526997d87400f33ad9912d511faf7a4/telegram-white.svg",
            "size": 1266
          }
        },
        "public": true,
        "created_at": "2020-12-11T12:58:59Z",
        "updated_at": "2020-12-11T12:59:46Z",
        "description": "",
        "comments": 0,
        "user": null,
        "comments_url": "https://api.github.com/gists/41da54b03986886e1a4e57c8cdb38dc2/comments",
        "owner": {
          "login": "cmschandan",
          "id": 21227329,
          "node_id": "MDQ6VXNlcjIxMjI3MzI5",
          "avatar_url": "https://avatars.githubusercontent.com/u/21227329?v=4",
          "gravatar_id": "",
          "url": "https://api.github.com/users/cmschandan",
          "html_url": "https://github.com/cmschandan",
          "followers_url": "https://api.github.com/users/cmschandan/followers",
          "following_url": "https://api.github.com/users/cmschandan/following{/other_user}",
          "gists_url": "https://api.github.com/users/cmschandan/gists{/gist_id}",
          "starred_url": "https://api.github.com/users/cmschandan/starred{/owner}{/repo}",
          "subscriptions_url": "https://api.github.com/users/cmschandan/subscriptions",
          "organizations_url": "https://api.github.com/users/cmschandan/orgs",
          "repos_url": "https://api.github.com/users/cmschandan/repos",
          "events_url": "https://api.github.com/users/cmschandan/events{/privacy}",
          "received_events_url": "https://api.github.com/users/cmschandan/received_events",
          "type": "User",
          "site_admin": false
        },
        "truncated": false
      },

标签: github-apigist

解决方案


如果您使用的是 JavaScript,那么您可以执行以下操作:

// `getGitHubGist` is a function that I assume you already 
// implemented which fetches your desired gist.
const gist = await getGitHubGist();

// Get an array of filename strings
const filenames = Object.keys(gist.files);

// Get an array of all the file details
const files = Object.values(gist.files);

推荐阅读