【问题标题】:JSONAPI boolean attributesJSONAPI 布尔属性
【发布时间】:2017-03-20 20:46:09
【问题描述】:

当您使用JSONAPI 时,您如何处理布尔属性?

例如:

{
    "type": "motors",
    "id": "1",
    "attributes": {
      "name": "V8",
      "working": "true"
    }
}

{
    "type": "motors",
    "id": "1",
    "attributes": {
      "name": "V8",
      "working": "1"
    }
}

另外,我认为这是更好的解决方案,但不要使用官方规范:

{
    "type": "motors",
    "id": "1",
    "attributes": {
      "name": "V8",
      "working": true
    }
}

【问题讨论】:

  • 我认为如果不将值括在双引号之间,您将无法逃脱; "true" 应该可以正常工作(前提是您将映射到具有适当 bool 属性的模型),并且它也比 "1" 更明确
  • jsonapi 规范没有说“属性总是像字符串?”

标签: specifications json-api


【解决方案1】:

前两个示例代表 strings,最后一个示例代表 JSON 上的 a boolean 值 (http://www.json.org/)

来自规范:

值可以是string in double quotesnumbertruefalsenullan objectan array。这些结构可以嵌套。`

如果你真的想要a boolean,那么使用最后一个例子。

JSON:API 什么都不说,因为规范本身在 JSON 之上(继承所有 JSON 规范)

【讨论】:

    猜你喜欢
    • 2016-04-05
    • 2022-12-03
    • 2010-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-22
    相关资源
    最近更新 更多