标准JSON API响应格式? [英] Standard JSON API response format?

查看:182
本文介绍了标准JSON API响应格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

执行标准或最佳实践构建JSON响应存在从API?显然,每个应用程序的数据是不同的,所以这么多,我不关心,而是响应样板,如果你愿意。我的意思的例子:

成功的请求:

  {
  成功:真实,
  有效载荷:{
    / *应用程序特定的数据将放在这里。 * /
  }
}

失败的请求:

  {
  成功:假的,
  有效载荷:{
    / *应用程序特定的数据将放在这里。 * /
  },
  错误:{
    code:123,
    消息:发生错误!
  }
}


解决方案

是的,有一对夫妇的标准(尽管在标准定义的一些自由)已经出现:


  1. JSON API - JSON API包括创建和更新资源为好,不只是响应

  2. JSEND - 简单,也许你已经在做的事情

  3. OData JSON协议 - 非常复杂

  4. HAL - OData的一样,但目标是成为的 HATEOAS 喜欢。

还有JSON API描述格式:

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:

Successful request:

{
  "success": true,
  "payload": {
    /* Application-specific data would go here. */
  }
}

Failed request:

{
  "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 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.

There are also JSON API description formats:

  • Swagger
    • JSON Schema (used by swagger but you could use it stand alone)
  • WADL in JSON
  • HAL because HATEOAS in theory is self describing.

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

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