简体   繁体   English

从 Google Apps 脚本中的 Alpha Vantage API 中提取 JSON 值

[英]Extracting JSON values from Alpha Vantage API in Google Apps Script

I am trying to use Google Apps Script and Alpha Vantage APIs to extract JSON values to eventually be placed in Google Sheets.我正在尝试使用 Google Apps Script 和 Alpha Vantage API 来提取 JSON 值,以最终放置在 Google 表格中。 For certain Alpha Vantage API endpoints, I'm having trouble extracting the values I want.对于某些 Alpha Vantage API 端点,我无法提取我想要的值。

Here is one of the JSON endpoints I want to work with:这是我想使用的 JSON 端点之一:

https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=IBM&apikey=demo https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=IBM&apikey=demo

Note that this is just one of their demo links.请注意,这只是他们的演示链接之一。

For this API I want to extract just the 'price' value.对于这个 API,我只想提取“价格”值。

{
"Global Quote": {
"01. symbol": "IBM",
"02. open": "128.9400",
"03. high": "130.9950",
"04. low": "127.7900",
"05. price": "130.0600",
"06. volume": "5835669",
"07. latest trading day": "2021-03-18",
"08. previous close": "129.0300",
"09. change": "1.0300",
"10. change percent": "0.7983%"
}
}

Another API I want to extract values from:另一个 API 我想从中提取值:

https://www.alphavantage.co/query?function=BALANCE_SHEET&symbol=IBM&apikey=demo https://www.alphavantage.co/query?function=BALANCE_SHEET&symbol=IBM&apikey=demo

What I have right now in the Google App Script editor is the very first step in this (new to JS), but not exactly where to go from here.我现在在 Google App Script 编辑器中所拥有的是第一步(JS 新手),但不是从这里到 go 的确切位置。 The way the objects are named in the first API (eg "05. price") is throwing me off because I don't think I can use dot notation to call that value due to its naming convention in the API.在第一个 API(例如“05.price”)中命名对象的方式让我失望,因为我认为我不能使用点符号来调用该值,因为它在 API 中的命名约定。 (Let me know if that is incorrect). (让我知道这是否不正确)。

function testAPI() {
  
  var api_url = "https://www.alphavantage.co/queryfunction=GLOBAL_QUOTE&symbol=IBM&apikey=demo";
  var json = UrlFetchApp.fetch(api_url);
  
  var obj = JSON.parse(json);
  
  Logger.log(obj);
}

Any help on this workflow is appreciated.对此工作流程的任何帮助表示赞赏。

function testAPI() {
  var api_url = "https://www.alphavantage.co/queryfunction=GLOBAL_QUOTE&symbol=IBM&apikey=demo";
  var json = UrlFetchApp.fetch(api_url);
  var obj = JSON.parse(json);
  //I  assume everything above this is all correct if it's not that's your problem
  Logger.log(obj["Global Quote"]["05. price"]);
}

Testing:测试:

function testAPI() {
  //var api_url = "https://www.alphavantage.co/queryfunction=GLOBAL_QUOTE&symbol=IBM&apikey=demo";
  //var json = UrlFetchApp.fetch(api_url);
  var json = '{"Global Quote": {"01. symbol": "IBM","02. open": "128.9400","03. high": "130.9950","04. low": "127.7900","05. price": "130.0600","06. volume": "5835669","07. latest trading day": "2021-03-18","08. previous close": "129.0300","09. change": "1.0300","10. change percent": "0.7983%"}}';
  var obj = JSON.parse(json);
  Logger.log(obj["Global Quote"]["05. price"]);
}

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

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