github-api - 如何从 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
},
解决方案
如果您使用的是 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);
推荐阅读
- php - 将多个序列化对象合并为一个?
- c++ - 在do while中检查字符的无限循环
- pytorch - Pytorch 代码错误....'NoneType' 对象没有属性'zero_''不支持的操作数类型 *:'float' 和 'NoneType''
- python - 如何检查列表中是否存在数字
- python - 如何 cv2.VideoCapture 从 BytesIO 或图像数组中读取
- vue.js - vue.js 基本功能无法正常工作
- terminology - CPLEX 报告的“应用用户削减”的含义?
- powerbi - 无法确定字段之间的关系
- windows - 命名应用程序 explorer.exe 时 FPS 低
- excel - 不要在 Excel 单元格中显示 #N/A,但也不要在图表中将其视为 0 值