json - 谷歌 json 风格指南:如何发送单项响应?
问题描述
规范中有一个items
节点表示它用于一系列项目,如分页项目、youtube 视频列表
如果我对单个项目有 GET 请求,应该如何格式化响应?
只针对数组中的一项?项目:[项目]
解决方案
我认为@tanmay_vijay 的回答不够正确或不够细致,因为在文档中的 YouTube 示例中,单个项目的响应似乎在数组中。
{
"apiVersion": "2.0",
"data": {
"updated": "2010-02-04T19:29:54.001Z",
"totalItems": 6741,
"startIndex": 1,
"itemsPerPage": 1,
"items": [
{
"id": "BGODurRfVv4",
"uploaded": "2009-11-17T20:10:06.000Z",
"updated": "2010-02-04T06:25:57.000Z",
"uploader": "docchat",
"category": "Animals",
"title": "From service dog to SURFice dog",
"description": "Surf dog Ricochets inspirational video ...",
"tags": [
"Surf dog",
"dog surfing",
"dog",
"golden retriever",
],
"thumbnail": {
"default": "https://i.ytimg.com/vi/BGODurRfVv4/default.jpg",
"hqDefault": "https://i.ytimg.com/vi/BGODurRfVv4/hqdefault.jpg"
},
"player": {
"default": "https://www.youtube.com/watch?v=BGODurRfVv4&feature=youtube_gdata",
"mobile": "https://m.youtube.com/details?v=BGODurRfVv4"
},
"content": {
"1": "rtsp://v5.cache6.c.youtube.com/CiILENy73wIaGQn-Vl-0uoNjBBMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp",
"5": "https://www.youtube.com/v/BGODurRfVv4?f=videos&app=youtube_gdata",
"6": "rtsp://v7.cache7.c.youtube.com/CiILENy73wIaGQn-Vl-0uoNjBBMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp"
},
"duration": 315,
"rating": 4.96,
"ratingCount": 2043,
"viewCount": 1781691,
"favoriteCount": 3363,
"commentCount": 1007,
"commentsAllowed": true
}
]
}
}
然而,它可能取决于请求中的目标资源。这就是它在竞争的 JSONAPI 标准中的方式。
从 JSONAPI 标准:
资源的逻辑集合必须表示为一个数组,即使它只包含一项或为空。
推荐阅读
- validation - 是否允许对输入元素的 v-model 和验证规则属性使用不同的名称?
- javascript - 为什么无法通过保存在 IndexedDB 中的 emscriptens fopen() 打开文件?
- c++ - 如何填充 std::vector
带文字 - c# - SkiaSharp:多次旋转大图而不会因为内存管理而导致应用程序崩溃
- php - 如何使用 PHP 创建随机密码生成器?
- javascript - 如何确定用户是从 Intranet 还是 Internet 连接
- ios - 有没有办法让 `genstrings` 与 `LocalizedStringKey` 一起工作?
- java - 微调器,为什么返回不同的间隙和错误的值
- android - Json删除前导零
- gitlab - 仅当两个文件已更改时如何运行 CI 作业?[不是一个或另一个]