如何用逗号将字符串追加到jquery数组中

时间:2014-06-07 08:39:30

标签: jquery json

我有像

这样的字符串
var json = "'a',9,8"

我希望将其添加到数组中,如

data.addRow([json]);  

data.addRow(['a',9,8]);工作正常但data.addRow([json]);却没有。

 function drawChart(term) {
     $.ajax({
         type: "POST",
         dataType: "json",
         url: "AssignGrade.aspx/Total_StudentToGradegraph",
         contentType: "application/json; charset=utf-8",
         global: false,
         async: false,
         data: '{term: "' + term + '"}',
         success: function (response) {
             var jsonData = jQuery.parseJSON(response.d);
             var data = new google.visualization.DataTable();
             // assumes "word" is a string and "count" is a number

             $.each(jsonData[0], function (key, value) {
                 if (key != "GradeName") {
                     data.addColumn('number', key);
                 } else {
                     data.addColumn('string', key);
                 }
             });

             var json = ""
             for (var j = 0; j < jsonData.length; j++) {
                 $.each(jsonData[j], function (key, value) {
                     if (value == null) {
                         value = 0;
                     }

                     if (key != "GradeName") {
                         value = parseInt(value)
                     } else {
                         value = "'" + value + "'"
                     }

                     json = json + value + ","
                 })
                 json = json.substr(0, json.length - 1);
                 data.addRow([json]);
             }

             var options = {
                 title: 'Grade details',
                 is3D: true
             };
             var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
             chart.draw(data, options);
         }
     });
}

data.addRow([json]);正在提出问题

1 个答案:

答案 0 :(得分:1)

在添加到数组之前,只需拆分字符串:

data.addRow(json.split(","));