简体   繁体   English

从文件导入 JSON - 不是 URL

[英]Import JSON From File - Not URL

I've been sent a JSON file which has data from an America's Cup yacht, for one race.我收到了一份 JSON 文件,其中包含来自美洲杯游艇的数据,用于一场比赛。 It has 22 'paths' if that's the right term, from the sensors onboard the yacht.如果这是正确的术语,它有 22 条“路径”,来自游艇上的传感器。

Every online recommendation leads back to the script from "ImportJSON" at GitHub.每个在线推荐都会从 GitHub 的“ImportJSON”返回脚本。 However, I have not been able to adapt the functions provided to import from a File, instead of accessing a JSON feed from a URL.但是,我无法调整提供的功能以从文件导入,而不是从 URL 访问 JSON 提要。

I also tried to import the JSON file as a text file, but it is too large, and threw an Exception.我也尝试将 JSON 文件作为文本文件导入,但是它太大了,并且抛出了异常。

A public version of my spreadsheet with the ImportJSON script is at ACWS , and a copy of the json file at boat1.json (55.8Mb) as well as a zipped file at boat1.zip (3.9Mb).带有 ImportJSON 脚本的电子表格的公共版本位于ACWS ,json 文件的副本位于boat1.json (55.8Mb) 以及压缩文件位于boat1.ZADCDBD79A8D84175C223.9B192AADCF175C223.9bM2

Any suggestions on how to import this JSON file would be really welcome.任何有关如何导入此 JSON 文件的建议都将受到欢迎。

To use any import function, you can't use a local file.要使用任何导入 function,您不能使用本地文件。 You have to use a URL.您必须使用 URL。 The download links from your Google file pages work just fine, though.不过,您的 Google 文件页面的下载链接可以正常工作。

Google currently limits URLFetchApp calls to 50MB, so a direct import is impossible. Google 目前将 URLFetchApp 调用限制为 50MB,因此无法直接导入。 Luckily, your zip file is much smaller than that, and the Apps Script API happens to have an unzip function.幸运的是,您的 zip 文件比这小得多,而 Apps 脚本 API 恰好有一个解压缩 function。

You can adapt this apps script to fit your needs:您可以调整此应用程序脚本以满足您的需求:

function ImportMyZipFile()
{
  // Your zip file URL:
  const url = 'https://drive.google.com/u/0/uc?id=1Zz5Oc_ZbK5Ohk0YRU6LxG2GdfEskf10_&export=download';

  let blob = UrlFetchApp.fetch(url).getBlob();
  // Need this or unzip will throw an error
  blob.setContentType('application/zip');

  // Get the only file in the zip archive
  let unzipped = Utilities.unzip(blob)[0];

  // Parse file contents to JS object
  let contents = unzipped.getDataAsString();
  let parsed = JSON.parse(contents);

  // Do what you want with parsed here...

  return /* data array here */;
}

parsed now holds your JavaScript object. parsed现在包含您的 JavaScript object。 To output to cells, call the function in the cell, and it will output the contents of your data array.将 output 到单元格中,在单元格中调用 function,它将 output 中的数据数组的内容。 I don't know exactly what data from the json file you want, but this should be enough to get you started.我不确切知道您想要 json 文件中的哪些数据,但这应该足以让您入门。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM