【问题标题】:Select an array member by name with a JSON Pointer使用 JSON 指针按名称选择数组成员
【发布时间】:2016-12-15 18:34:48
【问题描述】:

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

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

代替:

links/0/schema

我希望能够做到:

links/{title=GET}/schema

【问题讨论】:

  • JsonPointers 非常有限。对于这种查询,您可能需要查看 JsonPath。
  • 我非常震惊,这个问题的曝光率如此之低,而且现在看来,没有适当的实现来解决这个请求。默认情况下如何期望每个数组成员始终具有相同的索引?

标签: jsonpointer


【解决方案1】:

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

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;
    }
  }

【讨论】:

    【解决方案2】:

    Json Pointer defined in RFC6901 不允许您按名称选择数组成员。这是 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.
    

    【讨论】:

      猜你喜欢
      • 2013-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-21
      • 1970-01-01
      • 2014-12-28
      • 2010-10-15
      • 1970-01-01
      相关资源
      最近更新 更多