本文介绍了JSON API 响应格式有标准吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否存在用于构建来自 API 的 JSON 响应的标准或最佳实践?显然,每个应用程序的数据都是不同的,所以我不关心那么多,而是响应样板",如果你愿意的话.我的意思的一个例子:
Do standards or best practices exist for structuring JSON responses from an API? Obviously, every application's data is different, so that much I'm not concerned with, but rather the "response boilerplate", if you will. An example of what I mean:
请求成功:
{
"success": true,
"payload": {
/* Application-specific data would go here. */
}
}
请求失败:
{
"success": false,
"payload": {
/* Application-specific data would go here. */
},
"error": {
"code": 123,
"message": "An error occurred!"
}
}
推荐答案
是的,已经出现了几个标准(尽管在标准的定义上有一些自由):
Yes there are a couple of standards (albeit some liberties on the definition of standard) that have emerged:
- JSON API - JSON API 还包括创建和更新资源,而不仅仅是响应.
- JSend - 很简单,可能是您已经在做的事情.
- OData JSON 协议 - 非常复杂.
- HAL - 像 OData,但目标是成为 HATEOAS 喜欢.
- JSON API - JSON API covers creating and updating resources as well, not just responses.
- JSend - Simple and probably what you are already doing.
- OData JSON Protocol - Very complicated.
- HAL - Like OData but aiming to be HATEOAS like.
还有JSON API描述格式:
There are also JSON API description formats:
- 大摇大摆
- JSON Schema(由 swagger 使用,但您可以单独使用)
- Swagger
- JSON Schema (used by swagger but you could use it stand alone)
这篇关于JSON API 响应格式有标准吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!