在Json中包含具有相同id和类型的关系:api规范[关闭]

问题描述 投票:-3回答:1

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

"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。

php json-api
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.