簡體   English   中英

使用Google腳本解析Json數組

[英]Parse Json Array using Google Scripts

我正在嘗試解析此JSON數組,但是我不熟悉如何在Google腳本中循環遍歷JSON。 我在此API中收到價格變量的未定義錯誤。

API 鏈接

 function CBAPI() {

      // Link the script with a spreadsheet using the unique identifier found in the spreadsheet web address
      var ss = SpreadsheetApp.openById('16UqqC_MjnRfwbpREUcrcl7q69bUjzPgoUm6ZBMorizk');

      var APIPullSheet = ss.getSheetByName("APIPull");

      // Clear Columns A, B, C & D
      APIPullSheet.getRange('A2:F19999').clearContent();

      var url= "https://api.coinmarketcap.com/v2/ticker/132";
      var responseAPI = UrlFetchApp.fetch(url);
      var parcedData = JSON.parse(responseAPI.getContentText());


      var id = [];
      var price = [];


          id.push(['id']);
          price.push(['price']);

      id.push([parcedData.data.id]);
      price.push([parcedData.data.price]);



      idRange = APIPullSheet.getRange(1, 1, id.length, 1);  // Put isFrozen in column A
      idRange.setValues(id);

      priceRange = APIPullSheet.getRange(1, 2, price.length, 1);  // Put lowestAsk in column B
      priceRange.setValues(price);


      // Append Latest Data to End of the File
      var tableData = ss.getSheetByName("TableData");
      var rangeData = tableData.getRange("H1:K1");  
      var latestData = rangeData.getValues();   // Put I1 to O1 in latestData variable
      tableData.appendRow(latestData[0]);   // Put the data at the bottom of the spreadsheet

      // Keep 144 rows - Delete any extra starting at row 2
      var rowsToKeep = 5000;   // 5000 at request of Edwin
      var totalRows = tableData.getLastRow();
      var numToDelete = totalRows - rowsToKeep;
        if (numToDelete > 0)
           {
             tableData.deleteRows(2, numToDelete);  // Purge Extra Rows - Starting With Row 2 (oldest)
           }

    }

API返回quotes對象內的價格。

更換:

  price.push([parcedData.data.price]);

附:

  price.push([parcedData.data.quotes.USD.price]);

在此處輸入圖片說明

暫無
暫無

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

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