我使用雅虎財務API從它獲取數據,然後我想在谷歌圖表中輸入數據,但似乎我有一個問題JSON對象。所以,我嘗試了所有我讀過的東西,但不幸的是仍然沒有成功。
錯誤JSON從雅虎財經API SyntaxError當作爲參數給谷歌圖表表
index.html:1 Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse() at drawChart (main.js:18) at Object.google.a.c.Ac (loader.js:155) at Object.google.a.c.Pa (loader.js:155) at f (loader.js:152) at Object.google.l.m.kj (loader.js:229) at Object.google.l.m.ce (loader.js:229) at loader.js:228
的JavaScript
google.charts.load('current', {
'packages': ['corechart']
});
google.charts.setOnLoadCallback(drawChart);
var options = {
'title': 'Today is not my day',
'width': 400,
'height': 300
};
var input = getData();
function drawChart() {
var chartData = google.visualization.arrayToDataTable(JSON.parse(input));
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(chartData, options);
};
function getData() {
dataArray = [
['Name', 'Volume'],
];
var BASE_URL = "https://query.yahooapis.com/v1/public/yql?q=";
var yql_query = 'select * from yahoo.finance.quote where symbol in ("YHOO","AAPL","GOOG","MSFT")';
var yql_query_str = encodeURI(BASE_URL + yql_query);
var result = yql_query_str + "&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
$.getJSON(result, function(data){
var object = data.query.results.quote;
for (var i = 0; i < object.length; i++) {
var currentObj = object[i];
var pushedArray = [currentObj.Name, parseFloat(currentObj.Volume)];
dataArray[i+1] = pushedArray;
}
return dataArray;
});
};
HTML
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.min.css">
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- <script type="text/javascript" src="js/request.js"></script> -->
<script type="text/javascript" src="js/main.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12" id="chart_div"></div>
</div>
</div>
<script type="text/javascript" src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
</body>
</html>