从JSON创建数组数组

时间:2021-11-05 12:03:05

I am receiving after an ajax call the following as response using in php json_encode:

我在ajax调用后接收以下作为响应在php json_encode中使用:

"['2013-02-24', 0]", "['2013-02-25', 0]", "['2013-02-26', 1]", "['2013-02-27', 6]", "['2013-02-28', 6]", "['2013-03-01', 3]", ...

How can I make in JavaScript from this an array of arrays? Is this even possible? I mean, I've tried with jQuery makeArray or with parseJSON with no success. What is the most preferred method?

如何从这个数组数组中使用JavaScript?这有可能吗?我的意思是,我尝试过使用jQuery makeArray或使用parseJSON但没有成功。什么是最喜欢的方法?

Edit:

function submitForm(t) {
    $.ajax({type:'GET', url: 'charts.php', data:$(page_id).serialize(), success:
        function(response) {
            var myFanRemovesData = new Array(response);
            var myChart = new JSChart(chart_id, 'line');
            myChart.setDataArray(myFanRemovesData);

I have to use the array of arrays to set myFanRemovesData with it

我必须使用数组数组来设置myFanRemovesData

3 个解决方案

#1


3  

1) strip out the double-quotes ("):

1)删除双引号(“):

var json = json.replace(/"/g, '');

2) wrap the whole thing in square brackets:

2)将整个东西包在方括号中:

json = "[" + json + "]";

3) replace the single-quotes with double-quotes (because the singles won't parse):

3)用双引号替换单引号(因为单引号不会解析):

json = json.replace(/'/g, '"');

4) parse the json string:

4)解析json字符串:

var arrays = JSON.parse(json);

Here is a working example. It will alert the first date in the first array. (note: the data is pulled from the DIV to simulate the AJAX call and to avoid me having to mess around with escaping quote characters)

这是一个有效的例子。它将警告第一个数组中的第一个日期。 (注意:从DIV中提取数据以模拟AJAX调用并避免我不得不使用转义引号字符)

#2


2  

Try:

var response = ["['2013-02-24', 0]", "['2013-02-25', 0]", "['2013-02-26', 1]"];
for (var i = 0; i < response.length; i++) {
    var cleaned = response[i].replace(/'/g, "\"");
    response[i] = $.parseJSON(cleaned);
}

DEMO: http://jsfiddle.net/hu3Eu/

After this code, the response array will contain arrays, made out of the original strings.

在此代码之后,响应数组将包含由原始字符串构成的数组。

#3


1  

Just example.. because you haven't provide us with any code...

只是示例..因为您没有向我们提供任何代码......

$.ajax({
  type: "POST",
  url: "some.php",
  data: { name: "John", location: "Boston" },
  dataType: 'json',
}).done(function( responde ) {
    $.each(responde, function(i, v){ 
      alert(v.0 + ' --- ' + v.1);
    });
});

If you receive and expecting json you directly can use it as array/object :) If its array you have to make a each loop so you can access each value..

如果您收到并期待json,您可以直接使用它作为数组/对象:)如果它的数组你必须进行每个循环,这样你就可以访问每个值..

#1


3  

1) strip out the double-quotes ("):

1)删除双引号(“):

var json = json.replace(/"/g, '');

2) wrap the whole thing in square brackets:

2)将整个东西包在方括号中:

json = "[" + json + "]";

3) replace the single-quotes with double-quotes (because the singles won't parse):

3)用双引号替换单引号(因为单引号不会解析):

json = json.replace(/'/g, '"');

4) parse the json string:

4)解析json字符串:

var arrays = JSON.parse(json);

Here is a working example. It will alert the first date in the first array. (note: the data is pulled from the DIV to simulate the AJAX call and to avoid me having to mess around with escaping quote characters)

这是一个有效的例子。它将警告第一个数组中的第一个日期。 (注意:从DIV中提取数据以模拟AJAX调用并避免我不得不使用转义引号字符)

#2


2  

Try:

var response = ["['2013-02-24', 0]", "['2013-02-25', 0]", "['2013-02-26', 1]"];
for (var i = 0; i < response.length; i++) {
    var cleaned = response[i].replace(/'/g, "\"");
    response[i] = $.parseJSON(cleaned);
}

DEMO: http://jsfiddle.net/hu3Eu/

After this code, the response array will contain arrays, made out of the original strings.

在此代码之后,响应数组将包含由原始字符串构成的数组。

#3


1  

Just example.. because you haven't provide us with any code...

只是示例..因为您没有向我们提供任何代码......

$.ajax({
  type: "POST",
  url: "some.php",
  data: { name: "John", location: "Boston" },
  dataType: 'json',
}).done(function( responde ) {
    $.each(responde, function(i, v){ 
      alert(v.0 + ' --- ' + v.1);
    });
});

If you receive and expecting json you directly can use it as array/object :) If its array you have to make a each loop so you can access each value..

如果您收到并期待json,您可以直接使用它作为数组/对象:)如果它的数组你必须进行每个循环,这样你就可以访问每个值..