/ / JSON Apiのattributes要素にネストされたオブジェクトを含める必要がありますか? - json、json-api

JSON Apiのattributes要素にネストされたオブジェクトを含める必要がありますか? - 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"
}
}
]
}

データの属性が常にフラットであるか、属性には位置などのネストされたオブジェクトも含まれている必要があるかどうかはわかりません

"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"}
},

回答:

回答№1の場合は3

はい、見てみましょう: http://jsonapi.org/format/#document-resource-object-attributes

Complex data structures involving JSON objects and arrays are allowed as attribute values. However, any object that constitutes or is contained in an attribute MUST NOT contain a relationships or links member, as those members are reserved by this specification for future use.


回答№2の場合は0

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
)

)

)

)

ここにlocationは配列を含んでいるので、これは入れ子にされたオブジェクトになります。