谷歌表格JSON解析循环行动

时间:2017-09-25 14:57:51

标签: json google-sheets

工作表链接:https://docs.google.com/spreadsheets/d/1MLlKnlS-IB0OwWGIJ9tVI38pvqoCktd1HKlpofzn8cI/edit?usp=sharing

大家好。我是GAS中JSON解析的新手,希望有人可以提供帮助。它似乎应该是相当简单的东西,但我无法弄明白。

JSON包含我想要提取的2个属性,case_upc_14和salsify:created_at。我能够成功提取单个值,但我需要帮助创建一个循环来提取所有case_upc_14和salsify:created_at属性并返回导出选项卡上的值。

这就是我所拥有的,非常感谢任何帮助。

function parse() {

  var s = SpreadsheetApp.getActiveSpreadsheet(); 
  var t = s.getSheetByName('Sheet2'); 
  var response = UrlFetchApp.fetch('https://salsify-dandelion.s3-external-1.amazonaws.com/1/3889/user_data_export/1/2017/09/24/15%3A56%3A46-e227b249/export.json?response-content-disposition=attachment%3B%20filename%3D%22product-feed.json%22&response-content-type=application%2Fjson&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAJSAUHNZHZGLUDXDA%2F20170924%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20170924T160544Z&X-Amz-Expires=604800&X-Amz-SignedHeaders=host&X-Amz-Signature=b9407a842978363265472d27f4e8026506ef7d0a7e859a7c39feb0f4870fb9ad');

  var json = response.getContentText();
  var data = JSON.parse(json);

  Logger.log(data[4]['products'][1]['case_upc_14']);
  Logger.log(data[4]['products'][1]['salsify:created_at']);


}

1 个答案:

答案 0 :(得分:0)

以下修改怎么样?您似乎尝试将检索到的数据导入到电子表格中。因此,修改后的脚本会检索case_upc_14salsify:created_at,并将数据导入到电子表格的Sheet2

修改后的脚本:

function parse() {
  var s = SpreadsheetApp.getActiveSpreadsheet(); 
  var t = s.getSheetByName('Sheet2'); 
  var response = UrlFetchApp.fetch('https://salsify-dandelion.s3-external-1.amazonaws.com/1/3889/user_data_export/1/2017/09/24/15%3A56%3A46-e227b249/export.json?response-content-disposition=attachment%3B%20filename%3D%22product-feed.json%22&response-content-type=application%2Fjson&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAJSAUHNZHZGLUDXDA%2F20170924%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20170924T160544Z&X-Amz-Expires=604800&X-Amz-SignedHeaders=host&X-Amz-Signature=b9407a842978363265472d27f4e8026506ef7d0a7e859a7c39feb0f4870fb9ad');
  var json = response.getContentText();
  var data = JSON.parse(json);

  var ar = [];
  data[4].products.forEach(function(e) {
    ar.push([e['salsify:created_at'], e['case_upc_14']])
  });
  t.getRange(1,1,ar.length,ar[0].length).setValues(ar);
}

如果我误解了你的问题,我很抱歉。