简体   繁体   English

使用javascript以json格式获取yahoo finance的股票报价

[英]Get stock quotes from yahoo finance in json format using a javascript

I was trying to get stock quotes from yahoo api. 我试图从雅虎api获得股票报价。 My input to the query is only a stock ticker ( from a text field). 我对查询的输入只是一个股票代码(来自文本字段)。 On button click the background JavaScript method "getprice()" is called. 在按钮上单击后面的JavaScript方法“getprice()”被调用。 I have a java script code that looks like this 我有一个看起来像这样的java脚本代码

function getprice()
{
    var symbol = $('#stockquote').val();


    var url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22"+symbol+"%22)%0A%09%09&env=http%3A%2F%2Fdatatables.org%2Falltables.env&format=json";

    $.getJSON(url, function (json)
    {

        var lastquote = json.query.results.quote.LastTradePriceOnly;
        $('#stock').text(lastquote);

    });
}

 $('#stock').text(lastquote); 

Here "stock" is the text field where I want to display the LastTradePriceOnly for the given ticker. 这里“stock”是我希望显示给定股票代码的LastTradePriceOnly的文本字段。

I do not see any output turning up. 我没有看到任何输出出现。 Debugging also does not show up any errors. 调试也不会显示任何错误。 Can I get any suggestions with this issue? 我可以就此问题得到任何建议吗?

Try this. 试试这个。

function getData() {
    var url = 'http://query.yahooapis.com/v1/public/yql';
    var symbol = $("#symbol").val();
    var data = encodeURIComponent("select * from yahoo.finance.quotes where symbol in ('" + symbol + "')");

    $.getJSON(url, 'q=' + data + "&format=json&diagnostics=true&env=http://datatables.org/alltables.env")
        .done(function (data) {
            $('#result').text("Price: " + data.query.results.quote.LastTradePriceOnly);
        })
        .fail(function (jqxhr, textStatus, error) {
            var err = textStatus + ", " + error;
            console.log('Request failed: ' + err);
        });
}

Here I also added working example for you. 在这里,我还为您添加了工作示例。

This is how it's done in AngularJS in case you need it: 这是在AngularJS中完成的,以备不时之需:

In your view: 在你看来:

<section ng-controller='StockQuote'>
    <span>Last Quote: {{lang}}, {{lastTradeDate}}, {{lastTradeTime}}, {{lastTradePriceOnly}}</span>
</section><br>

In your controller: The stock symbol name is passed via $scope.ticker_name to service method 'getData.getStockQuote'. 在您的控制器中:股票代码名称通过$ scope.ticker_name传递给服务方法'getData.getStockQuote'。

appModule.controller('StockQuote', ['$scope', 'getData',
function($scope, getData) {
    var api = getData.getStockQuote($scope.ticker_name);
    var data = api.get({symbol:$scope.ticker_name}, function() {
        var quote = data.query.results.quote;
        $scope.lang = data.query.lang;
        $scope.lastTradeDate = quote.LastTradeDate;
        $scope.lastTradeTime = quote.LastTradeTime;
        $scope.lastTradePriceOnly = quote.LastTradePriceOnly;
    });
}]);

In your service: 在您的服务中:

appModule.service('getData', ['$http', '$resource', function($http, $resource) {
    // This service method is not used in this example.
    this.getJSON = function(filename) {
        return $http.get(filename);
    };
    // The complete url is from https://developer.yahoo.com/yql/.
    this.getStockQuote = function(ticker) {
        var url = 'http://query.yahooapis.com/v1/public/yql';
        var data = encodeURIComponent(
            "select * from yahoo.finance.quotes where symbol in ('" + ticker + "')");
        url += '?q=' + data + '&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys';
        return $resource(url);
    }
}]);

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

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