使用 JSON 指针按名称选择数组成员

jab*_*key 14 jsonpointer

有没有办法用 JSON 指针选择一个数组成员作为键的值?所以对于这个 JSON 模式:

"links":[
    {
      "title": "Create",
      "href": "/book",
      "method": "POST",
      "schema": {}
    },
    {
      "title": "Get",
      "href": "/book",
      "method": "GET",
      "schema": {}
    }
  ]
Run Code Online (Sandbox Code Playgroud)

代替:

links/0/schema
Run Code Online (Sandbox Code Playgroud)

我希望能够做到:

links/{title=GET}/schema
Run Code Online (Sandbox Code Playgroud)

Rod*_*nez 9

RFC6901 中定义的 Json 指针不允许您按名称选择数组成员。这是 RFC 中唯一提到数组的地方:

If the currently referenced value is a JSON array, the reference token MUST contain either:

* characters comprised of digits ..., or

* exactly the single character "-", making the new referenced
         value the (nonexistent) member after the last array element.
Run Code Online (Sandbox Code Playgroud)


jab*_*key 4

显然不是。所以我这样做了:

const links = schema.links;
  let ref;
  for (const [i, link] of links.entries()) {
    if (link.href === req.originalUrl && link.method === req.method) {
      ref = `schema.json#/links/${i}/schema`;
      break;
    }
  }
Run Code Online (Sandbox Code Playgroud)