簡體   English   中英

如何從嵌套的json創建一個數組

[英]how to make an array from nested json

我正在處理的 JSON 數據。 這來自 Facebook 廣告 API。 這是為谷歌數據工作室連接器構建的

這是我在谷歌應用腳​​本中使用的 JavaScript 代碼,但它顯示錯誤。 我知道這是完全錯誤的

 const parseData = { "data": [{ "adcreatives": { "data": [{ "actor_id": "8834759718540", "id": "538" }] }, "insights": { "data": [{ "ad_id": "34536578578", "impressions": "89108", "actions": [{ "action_type": "comment", "value": "02" }, { "action_type": "post", "value": "03" } ], "date_start": "2022-06-11", "date_stop": "2022-07-10" }], "paging": { "cursors": { "before": "MAZDZD", "after": "MAZDZD" } } }, "created_time": "2022-06-10T22:59:33+0600", "id": "34536578578" }, { "adcreatives": { "data": [{ "actor_id": "7834759718970", "id": "342" }] }, "insights": { "data": [{ "ad_id": "238509545896206", "impressions": "57803", "actions": [{ "action_type": "post_engagement", "value": "2102" }, { "action_type": "page_engagement", "value": "03" } ], "date_start": "2022-06-11", "date_stop": "2022-07-10" }], "paging": { "cursors": { "before": "MAZDZD", "after": "MAZDZD" } } }, "created_time": "2022-06-11T22:59:33+0600", "id": "238509545896206" } ], "paging": { "cursors": { "before": "dfgdfgdfgdfsdgdfgdfgdfgdfgdgdg", "after": "yuyuyuyutyuytuyutytynfrgersggsgs" } } }; var data = { data: parseData.data.map(({ actions, ...rest }) => ({ ...rest, ...Object.fromEntries(actions.map(({ action_type, value }) => [action_type, value])) })) }; console.log(data);

輸出應如下所示,以便我可以客觀地獲取所有數據

{
  "data": [
    {
      "actor_id": "8834759718540",
      "id": "538",
      "ad_id": "34536578578",
      "impressions": "89108",
      "comment": "02",
      "post": "03",
      "date_start": "2022-06-11",
      "date_stop": "2022-07-10",
      "created_time": "2022-06-10T22:59:33+0600"
    },
    {
      "actor_id": "7834759718970",
      "id": "342",
      "ad_id": "238512373324806",
      "impressions": "57803",
      "post_engagement": "2102",
      "page_engagement": "03",
      "date_start": "2022-06-11",
      "date_stop": "2022-07-10"
      "created_time": "2022-06-11T22:59:33+0600"
    }
  ],
 
}

不確定您應該如何處理嵌套數組,但對於特定數據,此代碼可以完成這項工作:

 const parseData = { "data": [ { "adcreatives": { "data": [{ "actor_id": "8834759718540", "id": "538" }] }, "insights": { "data": [{ "ad_id": "34536578578", "impressions": "89108", "actions": [{ "action_type": "comment", "value": "02" }, { "action_type": "post", "value": "03" } ], "date_start": "2022-06-11", "date_stop": "2022-07-10" }], "paging": { "cursors": { "before": "MAZDZD", "after": "MAZDZD" } } }, "created_time": "2022-06-10T22:59:33+0600", "id": "34536578578" }, { "adcreatives": { "data": [{ "actor_id": "7834759718970", "id": "342" }] }, "insights": { "data": [{ "ad_id": "238509545896206", "impressions": "57803", "actions": [{ "action_type": "post_engagement", "value": "2102" }, { "action_type": "page_engagement", "value": "03" } ], "date_start": "2022-06-11", "date_stop": "2022-07-10" }], "paging": { "cursors": { "before": "MAZDZD", "after": "MAZDZD" } } }, "created_time": "2022-06-11T22:59:33+0600", "id": "238509545896206" } ], "paging": { "cursors": { "before": "dfgdfgdfgdfsdgdfgdfgdfgdfgdgdg", "after": "yuyuyuyutyuytuyutytynfrgersggsgs" } } }; var new_data = []; for (let obj of parseData.data) { var new_obj = {}; new_obj.actor_id = obj.adcreatives.data[0].actor_id; new_obj.id = obj.adcreatives.data[0].id; new_obj.ad_id = obj.insights.data[0].ad_id; new_obj.impressions = obj.insights.data[0].impressions; var actions = obj.insights.data[0].actions; actions.forEach(a => new_obj[a.action_type] = a.value) new_obj.date_start = obj.insights.data[0].date_start; new_obj.date_stop = obj.insights.data[0].date_stop; new_obj.created_time = obj.created_time; new_data.push(new_obj) } console.log(new_data);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM