admin管理员组文章数量:1318564
I built out a simple request function to get a JSON from the Yahoo Finance API but am having trouble extracting data from the JSON.
Here's my function
var request = require("request");
var stock_url = ";view=%E2%80%8C%E2%80%8Bdetail";
request(stock_url, function (error, response, body) {
if (!error && response.statusCode == 200) {
var stock_data = body;
console.log("Yahoo Finance API: ", stock_data)
var stock_price = stock_data.list.resources[0].resource.fields.price;
console.log("stock_price: ", stock_price);
};
});
The JSON returned is stored in var stock_data
successfully and I then try to extract the "price" data with var stock_price = stock_data.list.resources[0].resource.fields.price
but am getting a TypeError: cannot read property 'resources' of undefined.
Below is the JSON, I've tried multiple derivatives for var stock_price
with no luck. Any help is much appreciated.
{
"list": {
"meta": {
"type": "resource-list",
"start": 0,
"count": 1
},
"resources": [
{
"resource": {
"classname": "Quote",
"fields": {
"name" : "Facebook, Inc.",
"price" : "116.620003",
"symbol" : "FB",
"ts" : "1465588800",
"type" : "equity",
"utctime" : "2016-06-10T20:00:00+0000",
"volume" : "18510826"
}
}
}
]
}
}
I built out a simple request function to get a JSON from the Yahoo Finance API but am having trouble extracting data from the JSON.
Here's my function
var request = require("request");
var stock_url = "http://finance.yahoo./webservice/v1/symbols/FB/quote?format=json&view=%E2%80%8C%E2%80%8Bdetail";
request(stock_url, function (error, response, body) {
if (!error && response.statusCode == 200) {
var stock_data = body;
console.log("Yahoo Finance API: ", stock_data)
var stock_price = stock_data.list.resources[0].resource.fields.price;
console.log("stock_price: ", stock_price);
};
});
The JSON returned is stored in var stock_data
successfully and I then try to extract the "price" data with var stock_price = stock_data.list.resources[0].resource.fields.price
but am getting a TypeError: cannot read property 'resources' of undefined.
Below is the JSON, I've tried multiple derivatives for var stock_price
with no luck. Any help is much appreciated.
{
"list": {
"meta": {
"type": "resource-list",
"start": 0,
"count": 1
},
"resources": [
{
"resource": {
"classname": "Quote",
"fields": {
"name" : "Facebook, Inc.",
"price" : "116.620003",
"symbol" : "FB",
"ts" : "1465588800",
"type" : "equity",
"utctime" : "2016-06-10T20:00:00+0000",
"volume" : "18510826"
}
}
}
]
}
}
Share
Improve this question
asked Jun 12, 2016 at 18:28
CBarCBar
531 gold badge1 silver badge3 bronze badges
3
- Need to parse it to an object first from string – charlietfl Commented Jun 12, 2016 at 18:32
-
when you do the next code
console.log(typeof body)
what is printed? – Daniel Krom Commented Jun 12, 2016 at 19:05 - @DanielKrom string is printed. Parsing it to an object worked. Thanks charlietfl – CBar Commented Jun 12, 2016 at 23:18
1 Answer
Reset to default 5Request returns body
as string value.
You should then parse it with var stock_data = JSON.parse(body)
.
BTW you can use existing yahoo-finance implementations for node and avoid to rewrite it you own.
本文标签: javascriptHow do I extract data from a Yahoo Finance JSON using nodejsStack Overflow
版权声明:本文标题:javascript - How do I extract data from a Yahoo Finance JSON using node.js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742049951a2418012.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论