0
我尝试解析一个JSON基于本指南中的PHP文件结构数据:谷歌图表JSON装载双引号
https://google-developers.appspot.com/chart/interactive/docs/php_example?hl=en
我已经创建了我的JSON的一个好办法,但是当我用这个代码它似乎不读取JSON得好:
function drawChart() {
var jsonData = $.ajax({
url: "loader.php",
dataType:"json",
async: false
}).responseText;
我认为这是因为在this example JavaScript对象没有引号一些麻烦的JSON的键双引号。那么我应该错过什么地方?
我的脚本代码:
<script type="text/javascript">
// Load the Visualization API and the piechart package.
google.load('visualization', '1.0', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
function drawChart() {
var jsonData = $.ajax({
dataType:"json",
url: "loader.php",
async: false
}).responseText;
// Create our data table out of JSON data loaded from server.
var data = new google.visualization.DataTable(jsonData);
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, {title: 'Results', width: 400, height: 240});
}
</script>
和我loader.php
回报是什么的源代码:
{"cols":
[{"label":"Sources","type":"string"},
{"label":"Count","type":"number"}],
"rows":
[{"c":[{"v":"web"},{"v":"4757"}]},
{"c":[{"v":" iPhone"},{"v":"4324"}]},
{"c":[{"v":"Android"},{"v":"3294"}]},
{"c":[{"v":"BlackBerry\u00ae"},{"v":"2336"}]},
{"c":[{"v":"Instagram"},{"v":"951"}]}
]}
注:图表负荷良好,没有错误,但不加载JSON。它说:其他100%。所以标签和行都不是正确的。
你确定它不会加载该JSON ?这些数字实际上并不是数字,因此Google无法显示饼图(因为没有可显示的值)。代码运行后显示的内容是什么? 'alert(data.getValue(1,1))'的警报是什么? – jmac