将 JSON 数据解析到 Google Sheet 时出现问题

问题描述 投票:0回答:1

我正在尝试使用 Crypto DEX api 提取交易所中每对的价格对名称和当前价格数据,并将其放入 Google Sheets 中的两列中,一列用于货币对名称,另一列用于价格。我已经使用 ChatGPT 将 DEX 团队的一些代码转换为与 Google Sheets 兼容的版本(通过 Google App Script),但数据缺少第二列,因此看起来解析数据时出现问题。

The original script is as follows:

curl -X POST https://api.hyperliquid.xyz/info \
     -H "Content-Type: application/json" \
     -d '{ "type": "allMids" }'

使用 ChatGPT 进行转换时,我得到:

function getHyperliquidData() {
  try {
    // Create the payload for the API request
    var payload = {
      type: 'allMids'
    };

    // Convert the payload to JSON string
    var payloadString = JSON.stringify(payload);

    // Set up options for the HTTP request
    var options = {
      method: 'post',
      contentType: 'application/json',
      payload: payloadString
    };

    // Make the API request
    var response = UrlFetchApp.fetch('https://api.hyperliquid.xyz/info', options);

    // Parse the API response
    var data = JSON.parse(response.getContentText());

    // Log the API response (optional)
    Logger.log('API Response:', data);

    // Write the data to the Google Sheet
    writePairsInfoToSheet(data);
  } catch (error) {
    Logger.log('Error fetching API data:', error);
    // Handle the error as needed
  }
}

function writePairsInfoToSheet(pairsInfo) {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

  // Clear existing data
  sheet.clear();

  // Write header row
  sheet.getRange(1, 1).setValue('Pair');
  sheet.getRange(1, 2).setValue('Last Price');

  // Start from the second row
  var row = 2;

  // Iterate through pairs and write data to the sheet
  for (var pair in pairsInfo) {
    sheet.getRange(row, 1).setValue(pair);
    sheet.getRange(row, 2).setValue(pairsInfo[pair].px); // Assuming the price is available as 'px'

    row++;
  }
}

结果按预期在第 1 列中显示货币对名称,但价格列为空。任何有关我如何修复它的帮助都会很棒。我查看了历史示例,它们似乎不太匹配,因为我没有收到任何错误,只是部分缺少数据。

google-apps-script google-sheets cryptography dex
1个回答
0
投票

答案是基于向 ChatGPT 显示 Logger API 的输出并使用响应来识别所需的正确属性名称,然后再次向其显示一个片段以帮助其正确地将信息解析到工作表中

© www.soinside.com 2019 - 2024. All rights reserved.