简体   繁体   English

如何调用从Yahoo Finance API收到的JSON数据?

[英]How to call JSON data received from the Yahoo Finance API?

When I use the Yahoo Finance API to retrieve stock quotes from a certain company. 当我使用Yahoo Finance API从某家公司检索股票报价时。

Using the following Javascript code, I was able to retrieve stock data: 使用以下JavaScript代码,我能够检索库存数据:

console.log(JSON.stringify(quotes[i], null, 2));

I get the following output in my console: 我在控制台中得到以下输出:

{
  "date": "2017-06-06T04:00:00.000Z",
  "open": 153.899994,
  "high": 155.809998,
  "low": 153.779999,
  "close": 154.449997,
  "adjClose": 154.449997,
  "volume": 26624900,
  "symbol": "AAPL"
}

So from here, I want to call on only the "close" index from that JSON object. 因此,从这里开始,我只想调用该JSON对象中的“关闭”索引。 To do this, I tried adding the following in my javascript: 为此,我尝试在javascript中添加以下内容:

  var stockClose = JSON.parse(quotes[i]);
  console.log(stockClose.close); 

This however does not console log the "close" index of the JSON object. 但是,这不会控制台记录JSON对象的“关闭”索引。 I know this is probably a really dumb mistake I am making and would be grateful if someone could help me debug this. 我知道这可能是我犯的一个非常愚蠢的错误,如果有人可以帮助我进行调试,将不胜感激。 How do I retrieve the "close" index from the quotes object ? 如何从引号对象中检索“关闭”索引?

JSON.stringify() converts a JavaScript value to a JSON string. JSON.stringify()将JavaScript值转换为JSON字符串。

JSON.parse() parses a JSON string, constructing the JavaScript value or object described by the string. JSON.parse()解析JSON字符串,构造该字符串描述的JavaScript值或对象。

Based on your console log information, we can see it is already a JSON object . 根据您的控制台日志信息,我们可以看到它已经是一个JSON对象 So we do not JSON.parse() to parse your quotes[i]. 因此,我们不使用JSON.parse()来解析您的引号[i]。

You can either use quotes[i].close or quotes[i]["close"] to access it. 您可以使用quotes [i] .closequotes [i] [“ close”]进行访问。

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

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