16

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

"links":[
    {
      "title": "Create",
      "href": "/book",
      "method": "POST",
      "schema": {}
    },
    {
      "title": "Get",
      "href": "/book",
      "method": "GET",
      "schema": {}
    }
  ]

代替:

links/0/schema

我希望能够做到:

links/{title=GET}/schema
4

2 回答 2

2

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

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;
    }
  }
于 2016-12-22T19:04:24.063 回答
2

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.
于 2020-09-21T12:40:09.013 回答