github API在http链接头中发送json结果的分页数据:

Link: <https://api.github.com/repos?page=3&per_page=100>; rel="next",
<https://api.github.com/repos?page=50&per_page=100>; rel="last"

由于github API不是唯一使用这种方法的API(我想),我想问一下,是否有人有一个有用的小片段来解析链接头(例如,将其转换为数组),以便我可以将其用于我的js应用程序.

我在谷歌上搜索了一下,但没有发现任何关于如何从json API解析分页的有用信息

推荐答案

The parse-link-header NPM module exists for this purpose; its source can be found on github under a MIT license (free for commercial use).

Installation is as simple as:

npm install parse-link-header

Usage looks like the following:

var parse = require('parse-link-header');
var parsed = parse('<https://api.github.com/repos?page=3&per_page=100>; rel="next", <https://api.github.com/repos?page=50&per_page=100>; rel="last"')

...之后有parsed.nextparsed.last等:

{ next:
   { page: '3',
     per_page: '100',
     rel: 'next',
     url: 'https://api.github.com/repos?page=3&per_page=100' },
  last:
   { page: '50',
     per_page: '100',
     rel: 'last',
     url: ' https://api.github.com/repos?page=50&per_page=100' } }

Json相关问答推荐

您可以使用Jolt对JSON执行OR条件吗

为什么JQ筛选器不将原始输入打印为$var|.';文档?

在Reaction中从JSON文件中筛选数组

使用 jolt 变换压平具有公共列 JSON 的复杂嵌套

从 oracle 数据库中的 json blob 打印值

如何将 JSON 文本作为参数传递给 powershell?

通过 xslt 将内部 json 转换为 xml 时遇到问题

判断golang中解析的json响应中是否存在所需的json键(不是值)

从 json 对象中过滤掉所有出现的给定属性

如何使用 gson 将数据保存在 json 文件中?

Rails:format.js 或 format.json,或两者兼而有之?

苗条的 JSON 输出

Select 什么数据类型json或者jsonb或者text

区分字符串和列表的 Pythonic 方式是什么?

杰克逊:反序列化 for each 值都具有正确类型的 Map

将 json 转换为 C# 数组?

处理 HTTP 请求正文中的可选 JSON 字段

如何使用 Jackson 的 objectMapper 反序列化接口字段?

FastAPI:如何将正文读取为任何有效的 json?

仅使用字符串和值解析 JSON 对象