本文介绍了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: