首页 > 解决方案 > 如何在 JSONAPI 中呈现空关系?

问题描述

查看了http://jsonapi.org/format/但没有看到任何关于空关系格式的描述,例如:

{
  "type": "articles",
  "id": "1",
  "attributes": {
    "title": "Rails is Omakase"
  },
  "relationships": {
    "comments": {
        "data": []
      }
  }
}

article没有comments,什么是呈现空关系的正确方法?

"data": []或者"data": null根本没有"relationships"

谢谢!

标签: json-api

解决方案


It's described in resource linkage chapter of JSON API specification:

Resource linkage MUST be represented as one of the following:

  • null for empty to-one relationships.
  • an empty array ([]) for empty to-many relationships.
  • a single resource identifier object for non-empty to-one relationships.
  • an array of resource identifier objects for non-empty to-many relationships.

Note that you don't have to use resource linkage at all. You could also use relationship links. You could find more information about that one in this part of specification.


推荐阅读