如何反序列化具有相同ID的JSON API响应?

huangapple go评论76阅读模式
英文:

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 &#39;jsona&#39;;


const jsona = new Jsona();


const data = {
  &quot;meta&quot;: {
    &quot;page&quot;: {
      &quot;current-page&quot;: 1,
      &quot;per-page&quot;: 25,
      &quot;from&quot;: 1,
      &quot;to&quot;: 2,
      &quot;total&quot;: 2,
      &quot;last-page&quot;: 1
    }
  },
  &quot;data&quot;: [{
      &quot;type&quot;: &quot;brands&quot;,
      &quot;id&quot;: &quot;105&quot;,
      &quot;attributes&quot;: {
        &quot;name&quot;: &quot;Brand A&quot;,
        &quot;status&quot;: &quot;pending&quot;,
        &quot;company_id&quot;: 1
      },
    },
    {
      &quot;type&quot;: &quot;brands&quot;,
      &quot;id&quot;: &quot;105&quot;,
      &quot;attributes&quot;: {
        &quot;name&quot;: &quot;Brand A&quot;,
        &quot;status&quot;: &quot;approved&quot;,
        &quot;company_id&quot;: 2
      },
    }
  ]
}

const test = jsona.deserialize(data)
console.log(&#39;result&#39;, test);

<!-- end snippet -->

But the result I get is this:

[{
  type: &quot;brands&quot;
  id: &quot;105&quot;
  name: &quot;Brand A&quot;
  status: &quot;pending&quot;
  company_id: 1
},
{
  type: &quot;brands&quot;
  id: &quot;105&quot;
  name: &quot;Brand A&quot;
  status: &quot;pending&quot;
  company_id: 1
}]

答案1

得分: 1

你的服务器未生成有效的 JSON:API 响应,因为 id 应该对于特定的资源对象 type 是唯一的。

根据 JSON:API 规范:https://jsonapi.org/format/#document-resource-object-identification

在给定的 API 中,每个资源对象的类型和 id 对必须标识一个单一且唯一的资源。(由服务器或多个充当一体的服务器控制的 URI 集合构成一个 API。)

通常情况下,你应该建模两个资源,brandcompany,并且会存在从 brandcompany 的多对多关系。

然后查询应该是对 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

huangapple
  • 本文由 发表于 2023年7月14日 02:38:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76682358.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定