JSON Api 属性元素是否应该包含嵌套对象?

mko*_*mko 4 json json-api

这是我们第一次在我们的项目中使用 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"
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

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

"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"}
        },
Run Code Online (Sandbox Code Playgroud)

edy*_*chk 5

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

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