简体   繁体   English

Google Chart工具和JSON

[英]Google Chart Tools and JSON

On this example https://google-developers.appspot.com/chart/interactive/docs/quick_start The method data.addRows() takes a list of lists. 在此示例中, https : data.addRows()方法data.addRows()获取列表列表。

I have a URI (/data/mydata.json) that returns this data 我有一个URI(/data/mydata.json)返回此数据

[["Canada", 66], ["Turkey", 10], ["Hungary", 23], ["Italy", 49]]

Using JQuery's $.parseJSON(), I get the error SyntaxError: JSON.parse: unexpected character 使用JQuery的$ .parseJSON(),我收到错误SyntaxError: JSON.parse: unexpected character

How do I get that data into that method in the format it wants? 如何将数据以所需的格式导入该方法?

UPDATE 更新

I ran just alert($.parseJSON('/data/mydata.json')) and it parsed and displayed the data just fine. 我只运行了alert($.parseJSON('/data/mydata.json')) ,它解析并显示了数据。 It seems like data.addRows() is the one throwing the error. 似乎data.addRows()是引发错误的代码。

Here is the full code: 这是完整的代码:

google.load('visualization', '1.0', {'packages':['corechart']});
google.setOnLoadCallback(drawChart)
function drawChart(){
var data = new google.visualization.DataTable();
data.addColumn('string', 'Country');
data.addColumn('number', 'Node Count');
data.addRows($.parseJSON('/data/mydata.json'));

var options = {'title':'Tor Nodes by Country',
           'width':800,
           'height':600};

var chart = new google.visualization.PieChart(document.getElementById('nodes'));
chart.draw(data, options);

} }

JQuery perfectly parses the JSON data. JQuery完美解析JSON数据。 Output is an array of arrays. 输出是一个数组数组。

Here is an JS example how it works: 这是一个JS示例,其工作方式:

var json_data = "[[\"Canada\", 66], [\"Turkey\", 10], [\"Hungary\", 23], [\"Italy\", 49]]";
var parsed_data = $.parseJSON(json_data);

document.write('<table><tr><th>City</th><th>number</th></tr>');
for (var i=0; i < parsed_data.length; i++) {
    document.write('<tr><td>' + parsed_data[i][0] + '</td>');
    document.write('<td>' + parsed_data[i][1] + '</td></tr>');
}
document.write('</table>');

Are you sure that since you don't tell how you retrieve data, are you sure that JQuery didn't parsed input as a JSON when retrieving it? 您是否确定由于不告诉您如何检索数据,所以确定JQuery在检索输入时未将输入解析为JSON吗?

Here is a JSFiddle link to show contents of the json_data 这是一个JSFiddle链接,用于显示json_data的内容

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

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