【发布时间】:2019-09-14 23:31:00
【问题描述】:
我发送这样的响应(执行此响应的代码是正确的,我不需要代码):
"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 和 types 所以这个响应是不正确的,但我需要几个参与者可以属于同一个组织。我想显示组织属性,因为请求资源的人需要这些数据。不知道下一个结构是否正确:
"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。
【问题讨论】:
-
为什么 JSON API 会关心不同对象中的重复值?您能否请edit您的问题并将您的代码提供为minimal reproducible example?您可能还想先阅读How to Ask。
-
重复的 id 和类型 根据规范不正确 jsonapi.org/format 抱歉,我不知道还能提供什么,我发布了一个我所做的示例,但它不符合规范我需要将此响应“转换”为有效响应