簡體   English   中英

如何在 Google Apps 腳本中解析花括號 JSON?

[英]How to parse curly braces JSON in Google Apps Script?

我有來自 API 的以下 output。如何使用 Google Apps 腳本解析它並將 output 作為 Google 表格表格?

我嘗試了以下,但只得到 Nulls。

var response = UrlFetchApp.fetch(url, options);
var text = response.getContentText();

for (i in text) {
  Logger.log(text[i]);
}
{
   "status":1,
   "complete":1,
   "list":{
      "3357271691":{
         "item_id":"3357271691",
         "resolved_id":"3357271691",
         "given_url":"https:\/\/www.bloomberg.com\/news\/features\/2021-06-15\/airbnb-spends-millions-making-nightmares-at-live-anywhere-rentals-go-away",
         "given_title":"",
         "favorite":"0",
         "status":"0",
         "time_added":"1623783338",
         "time_updated":"1623783338",
         "time_read":"0",
         "time_favorited":"0",
         "sort_id":0,
         "resolved_title":"Airbnb Is Spending Millions of Dollars to Make Nightmares Go Away",
         "resolved_url":"https:\/\/www.bloomberg.com\/news\/features\/2021-06-15\/airbnb-spends-millions-making-nightmares-at-live-anywhere-rentals-go-away",
         "excerpt":"When things go horribly wrong during a stay, the company\u2019s secretive safety team jumps in to soothe guests and hosts, help families\u2014and prevent PR disasters. Megaphone.fm: How Airbnb Makes Nightmares Disappear...",
         "is_article":"1",
         "is_index":"0",
         "has_video":"0",
         "has_image":"1",
         "word_count":"155",
         "lang":"en",
         "top_image_url":"https:\/\/assets.bwbx.io\/images\/users\/iqjWHBFdfxIU\/iWjqS9z4c46I\/v0\/1200x630.jpg",
         "domain_metadata":{
            "name":"Bloomberg",
            "logo":"https:\/\/logo.clearbit.com\/bloomberg.com?size=800",
            "greyscale_logo":"https:\/\/logo.clearbit.com\/bloomberg.com?size=800&greyscale=true"
         },
         "listen_duration_estimate":60
      }
   }
}

我相信你的目標如下。

  • 您想要解析檢索到的text值並將數據放入電子表格。

在這種情況下,下面的示例腳本怎么樣? 在此示例腳本中,對值進行解析並創建一個二維數組,並將該數組放入電子表格。

示例腳本:

function myFunction() {
  var response = UrlFetchApp.fetch(url, options);
  var text = response.getContentText();

  // I added below script.
  var header = ["item_id", "resolved_id", "given_url","given_title","favorite","status","time_added","time_updated","time_read","time_favorited","sort_id", "resolved_title","resolved_url","excerpt","is_article","is_index","has_video","has_image","word_count","lang","top_image_url", "domain_metadata.name","domain_metadata.logo","domain_metadata.greyscale_logo","listen_duration_estimate"];
  var values = [header, ...Object.values(JSON.parse(text).list).map(o => header.map(k => {
    var temp = k.split(".");
    if (temp.length == 1) {
      return o[temp[0]] || "";
    }
    return o[temp[0]] && o[temp[0]][temp[1]] ? o[temp[0]][temp[1]] : "";
  }))];
  console.log(values) // You can check the created array.
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Articles"); // Please set the sheet name.
  sheet.getRange(1, 1, values.length, values[0].length).setValues(values);
}
  • 在這種情況下,所有值都被放入電子表格。 所以,當你要更改header時,請修改它。

筆記:

  • 此示例腳本假定您的text值與問題中顯示的值相同。 當值的結構不同時,可能無法使用此腳本。 請注意這一點。

參考:

暫無
暫無

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

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