本文介绍了通过jquery ajax获取javascript数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许你可以帮助我。

我的jquery ajax调用结果分为||并创建一个值数组。这些值之一是这样的字符串:

My jquery ajax call result is splitted with "||" and it creates an array of values. One of these values is a string like this:

[['2012-11-18', 33, 2], ['2012-11-19', 162, 11], ['2012-11-20', 140, 13]]

我需要把它作为一个可行的javascript数组传递给google图表drawchart(myarray)函数使用data.addRows(myarray);

I need to make this as a viable javascript array to pass it to google chart drawchart(myarray) function to use it with data.addRows(myarray);

任何人都可以帮助我实现它?

Can anyone help me to achieve it?

我可以使这个ajax返回数组看起来像什么,重要的是,我可以使用这个数组更新Google图表。

I can make this ajax returned array to look like whatever, what is important is that i could use this array to update google chart.

好的,继续我的ajax成功代码:

OKay, heres my ajax success code:

success: function(response)
     {
        response_d = response.split("||");
        response_message = response_d[0];

        if(response_message == 'ok') {
           $("#contr_count").html(response_d[1]);
           $("#contr_average").html(contr_time(response_d[2]));
           $("#contr_space_average").html(contr_time(response_d[3]));
           var chartdata = $.parseJSON(response_d[4]);
           drawChart(chartdata);
           $("#contr_list").html(response_d[5]);
        }
        else {

        }
     }


$ b b

这里是drawchart函数

And here is the drawchart function

function drawChart(chartdata) {
 var data = new google.visualization.DataTable();
  data.addColumn('string', 'Minutit tagasi');
  data.addColumn('number', 'T pikkus');
  data.addColumn('number', 'T vahe pikkus');

  data.addRows(chartdata);

data.addRows(dateArray);
 var options = {
   title:                 'T pikkuse ja vahe graafik (viimane 60 min)',
   backgroundColor:          '#fdf3e9',
   colors:                ['#FF7F00','#437C17'],
   curveType:             'function',
   enableInteractivity:      true,
   legend:                {position: 'bottom'},
   vAxis:                 {direction: -1},
   hAxis:                 {direction: -1},
   chartArea:             {width: '85%'}
 };

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

}

推荐答案

您需要替换'第一个。
尝试:

You need to replace ' with " first.Try with:

var s = "[['2012-11-18', 33, 2], ['2012-11-19', 162, 11], ['2012-11-20', 140, 13]]";
var arr = $.parseJSON(s.replace(/'/g,'"'))


打开javascript控制台查看结果。

Here a jsfiddle with a working example.Open the javascript console to see the results.

这篇关于通过jquery ajax获取javascript数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 21:44