【问题标题】:Should JSON Api attributes element contain nested objects?JSON Api 属性元素是否应该包含嵌套对象?
【发布时间】:2017-06-11 19:09:05
【问题描述】:

这是我们第一次在我们的项目中使用 JSON API,根据他们网站上的规范,这是常规 JSON API 响应的样子

HTTP/1.1 200 OK
Content-Type: application/vnd.api+json

{
  "data": [{
    "type": "articles",
    "id": "1",
    "attributes": {
      "title": "JSON API paints my bikeshed!",
      "body": "The shortest article. Ever.",
      "created": "2015-05-22T14:56:29.000Z",
      "updated": "2015-05-22T14:56:28.000Z"
    },
    "relationships": {
      "author": {
        "data": {"id": "42", "type": "people"}
      }
    }
  }],
  "included": [
    {
      "type": "people",
      "id": "42",
      "attributes": {
        "name": "John",
        "age": 80,
        "gender": "male"
      }
    }
  ]
}

我们不确定,数据中的属性是否应该总是扁平的,或者属性也可以包含嵌套对象,例如位置

"data": [{
        "type": "articles",
        "id": "1",
        "attributes": {
          "title": "JSON API paints my bikeshed!",
          "body": "The shortest article. Ever.",
          "created": "2015-05-22T14:56:29.000Z",
          "updated": "2015-05-22T14:56:28.000Z",
          "location": 
          { 
             "lat": "0.00",
             "long": "0.00"}
        },

【问题讨论】:

    标签: json json-api


    【解决方案1】:

    是的,看看:http://jsonapi.org/format/#document-resource-object-attributes

    允许涉及 JSON 对象和数组的复杂数据结构 作为属性值。然而,任何构成或是 包含在属性中不得包含关系或链接 成员,因为这些成员由本规范保留以供将来使用 使用。

    【讨论】:

      【解决方案2】:

      解码你的 JSON 后,结果是 -

      Array
      (
          [data] => Array
              (
                  [0] => Array
                      (
                          [type] => articles
                          [id] => 1
                          [attributes] => Array
                              (
                                  [title] => JSON API paints my bikeshed!
                                  [body] => The shortest article. Ever.
                                  [created] => 2015-05-22T14:56:29.000Z
                                  [updated] => 2015-05-22T14:56:28.000Z
                                  [location] => Array
                                      (
                                          [lat] => 0.00
                                          [long] => 0.00
                                      )
      
                              )
      
                          [relationships] => Array
                              (
                                  [author] => Array
                                      (
                                          [data] => Array
                                              (
                                                  [id] => 42
                                                  [type] => people
                                              )
      
                                      )
      
                              )
      
                      )
      
              )
      
          [included] => Array
              (
                  [0] => Array
                      (
                          [type] => people
                          [id] => 42
                          [attributes] => Array
                              (
                                  [name] => John
                                  [age] => 80
                                  [gender] => male
                              )
      
                      )
      
              )
      
      )
      

      这里的位置包含一个数组,所以这将是一个嵌套对象。

      【讨论】:

      • 我的问题是 json api 是否推荐属性元素内的嵌套对象
      • 是的! json api 推荐嵌套对象。
      • 你怎么知道的?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-27
      • 2021-11-20
      • 2023-03-12
      相关资源
      最近更新 更多