英文:
How to deserialize JSON API response with two the same id?
问题
我从PHP以JSON API格式接收到响应数据。我使用这个库jsona对接收到的数据进行反序列化,但在反序列化后,第二个数据被覆盖为前一个数据的内容。我该如何修复这个问题?
提示:这是一个查询结果,来自brand_table的一个品牌,并且与company_table进行了左连接,所以一个品牌可以在两个不同的公司中,我的目的是在前端显示它们,如下所示:
| 品牌 | 公司 |
| Brand A | 公司 Z |
| Brand A | 公司 Y |
查询:
SELECT brands.*, company_brands.company_id, company_brands.status FROM brands LEFT JOIN company_brands ON company_brands.brand_id = brands.id
import Jsona from 'jsona';
const jsona = new Jsona();
const data = {
"meta": {
"page": {
"current-page": 1,
"per-page": 25,
"from": 1,
"to": 2,
"total": 2,
"last-page": 1
}
},
"data": [{
"type": "brands",
"id": "105",
"attributes": {
"name": "Brand A",
"status": "pending",
"company_id": 1
},
},
{
"type": "brands",
"id": "105",
"attributes": {
"name": "Brand A",
"status": "approved",
"company_id": 2
},
}
]
}
const test = jsona.deserialize(data)
console.log('result', test);
但我得到的结果是这样的:
[{
type: "brands",
id: "105",
name: "Brand A",
status: "pending",
company_id: 1
},
{
type: "brands",
id: "105",
name: "Brand A",
status: "pending",
company_id: 1
}]
英文:
I have a response data from php in JSON API format. The data I received I deserialize it using this library jsona, but after deserializing it the second data gets overwritten with what is in the previous. How do I fix this?
Hint: This is a result of query, a brand from brand_table, and left join to a company_table, so a single brand can be in two different company which my purpose is to display it in the front end like this:
| Brand | Company |
| Brand A | Company Z |
| Brand A | Company Y |
Query:
SELECT brands.*, company_brands.company_id, company_brands.status FROM brands LEFT JOIN company_brands ON company_brands.brand_id = brands.id
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
import Jsona from 'jsona';
const jsona = new Jsona();
const data = {
"meta": {
"page": {
"current-page": 1,
"per-page": 25,
"from": 1,
"to": 2,
"total": 2,
"last-page": 1
}
},
"data": [{
"type": "brands",
"id": "105",
"attributes": {
"name": "Brand A",
"status": "pending",
"company_id": 1
},
},
{
"type": "brands",
"id": "105",
"attributes": {
"name": "Brand A",
"status": "approved",
"company_id": 2
},
}
]
}
const test = jsona.deserialize(data)
console.log('result', test);
<!-- end snippet -->
But the result I get is this:
[{
type: "brands"
id: "105"
name: "Brand A"
status: "pending"
company_id: 1
},
{
type: "brands"
id: "105"
name: "Brand A"
status: "pending"
company_id: 1
}]
答案1
得分: 1
你的服务器未生成有效的 JSON:API 响应,因为 id
应该对于特定的资源对象 type
是唯一的。
根据 JSON:API 规范:https://jsonapi.org/format/#document-resource-object-identification
在给定的 API 中,每个资源对象的类型和 id 对必须标识一个单一且唯一的资源。(由服务器或多个充当一体的服务器控制的 URI 集合构成一个 API。)
通常情况下,你应该建模两个资源,brand
和 company
,并且会存在从 brand
到 company
的多对多关系。
然后查询应该是对 brand
的查询,并使用 Compound Documents 功能来同时包含 company
的数据,可能使用 Sparse Fields 来选择你所需的特定数据字段。
/brand?include=company
英文:
Your server isn't generating a valid JSON:API response as the id
should be unique for a particular resource object type
.
From the JSON:API specification: https://jsonapi.org/format/#document-resource-object-identification
> Within a given API, each resource object’s type and id pair MUST identify a single, unique resource. (The set of URIs controlled by a server, or multiple servers acting as one, constitute an API.)
Generally you should be modelling two resources, brand
and company
and there would be a many-to-many relationship from brand
to company
.
The query would then be for brand
and use the Compound Documents feature to also include data for company
probably using Sparse Fields to select the specific data fields you require.
/brand?include=company
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论