JSON API响应格式是否有任何标准? [英] Is there any standard for JSON API response format?

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

  1. JSON API -JSON API还涵盖了创建和更新资源,而不仅仅是响应.
  2. JSend -简单,可能已经在做.
  3. HAL -与OData类似,但旨在成为
  1. JSON API - JSON API covers creating and updating resources as well, not just responses.
  2. JSend - Simple and probably what you are already doing.
  3. OData JSON Protocol - Very complicated.
  4. HAL - Like OData but aiming to be HATEOAS like.

还有JSON API描述格式:

There are also JSON API description formats:

  • 挥舞
    • JSON架构(由swagger使用,但您可以单独使用它)
    • Swagger
      • JSON Schema (used by swagger but you could use it stand alone)

      这篇关于JSON API响应格式是否有任何标准?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆