json-api - 如何在 JSONAPI 中呈现空关系?
问题描述
查看了http://jsonapi.org/format/但没有看到任何关于空关系格式的描述,例如:
{
"type": "articles",
"id": "1",
"attributes": {
"title": "Rails is Omakase"
},
"relationships": {
"comments": {
"data": []
}
}
}
这article
没有comments
,什么是呈现空关系的正确方法?
"data": []
或者"data": null
根本没有"relationships"
?
谢谢!
解决方案
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.
推荐阅读
- swift - 有没有一种优雅的方法来确定两个日期是否在 Swift 中被一年中的一周分开?
- spring - 多模块Maven项目中的应用程序上下文 - Spring
- javascript - 在 Django 中使用 javascript 删除禁用的属性
- html - 如何在伪提示(工具提示)中制作文本宽度?
- image - 在 Markdown 中对齐图像
- xml - 将 XML 转换为 SVG
- javascript - 如果文件系统上的 JavaScript 文件发生更改,节点进程是否受到保护?
- java - 我为 Rubymine 创建的插件不兼容
- python - Tensorflow 对象检测:ImportError: cannot import name 'inception_resnet_v2'
- json - 如何从数据库存储库中获取 JSON 格式的数据