Json:api规范中包含具有相同ID和类型的关系

时间:2019-04-25 13:49:24

标签: php json-api

我发送了这样的回复(执行此回复的代码是正确的,我不需要该代码):

"data": [
        {
            "type": "participant",
            "id": "c15409",
            "attributes": {
                "licenseNumber": "",
                "givenName": "Valeria Alejandra",
                "familyName": "Snchez Arredondo",
                "passportGivenName": "VALERIA ALEJANDRA",
            },
            "relationships": {
                "organization": {
                    "data": {
                        "type": "organization",
                        "id": "1205"
                    }
                }
            }
        },
        {
            "type": "participant",
            "id": "c16733",
            "attributes": {
                "licenseNumber": "",
                "givenName": "Amir",
                "familyName": "Mota Castaeda",
                "passportGivenName": "AMIR",
            },
            "relationships": {
                "organization": {
                    "data": {
                        "type": "organization",
                        "id": "1205"
                    }
                }
            }
        },

"included": [
    {
        "type": "organization",
        "id": "1205",
        "attributes": {
             "name": "Team",
             "country": "USA"
         }
    },
{
        "type": "organization",
        "id": "1205",
        "attributes": {
             "name": "Team",
             "country": "USA"
         }
    },
]

Json:api不允许使用相同的ID和类型,因此此响应不正确,但是我需要几个参与者可以属于同一组织。我想显示组织属性,因为请求资源的人需要这些数据。我不知道下一个结构是否正确:

"data": [
        {
            "type": "participant",
            "id": "c15409",
            "attributes": {
                "licenseNumber": "",
                "givenName": "Valeria Alejandra",
                "familyName": "Snchez Arredondo",
                "passportGivenName": "VALERIA ALEJANDRA",
            },
            "relationships": {
                "organization": {
                    "data": {
                        "type": "organization",
                        "id": "1205"
                    }
                }
            }
        },
        {
            "type": "participant",
            "id": "c16733",
            "attributes": {
                "licenseNumber": "",
                "givenName": "Amir",
                "familyName": "Mota Castaeda",
                "passportGivenName": "AMIR",
            },
            "relationships": {
                "organization": {
                    "data": {
                        "type": "organization",
                        "id": "1205"
                    }
                }
            }
        },

"included": [
    {
        "type": "organization",
        "id": "1205",
        "attributes": {
             "name": "Team",
             "country": "USA"
         }
    }

我的母语不是英语,但我认为很明显我不是在问为什么这段代码行不通。我只想知道我已经完成的结构是正确的。是或否是有效答案。无论如何,要感谢赖安(Ryan)回答了我。

1 个答案:

答案 0 :(得分:1)

也许您的json结构不正确,请检查json结构是否符合您的需求。

{
    "data":[
        {
            "type":"participant",
            "id":"c15409",
            "attributes":{
                "licenseNumber":"",
                "givenName":"Valeria Alejandra",
                "familyName":"Snchez Arredondo",
                "passportGivenName":"VALERIA ALEJANDRA"
            },
            "relationships":{
                "organization":{
                    "data":{
                        "type":"organization",
                        "id":"1205"
                    }
                }
            }
        },
        {
            "type":"participant",
            "id":"c16733",
            "attributes":{
                "licenseNumber":"",
                "givenName":"Amir",
                "familyName":"Mota Castaeda",
                "passportGivenName":"AMIR"
            },
            "relationships":{
                "organization":{
                    "data":{
                        "type":"organization",
                        "id":"1205"
                    }
                }
            }
        }
    ],
    "included":[
        {
            "type":"organization",
            "id":"1205",
            "attributes":{
                "name":"Team",
                "country":"USA"
            }
        }
    ]
}

请参阅正式文档:https://jsonapi.org/format/#document-compound-documents