最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Create array of arrays from JSON - Stack Overflow

programmeradmin10浏览0评论

I am receiving after an ajax call the following as response using in 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?

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

I am receiving after an ajax call the following as response using in 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?

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

Share Improve this question edited Apr 24, 2013 at 13:16 Daniela costina Vaduva asked Apr 24, 2013 at 13:10 Daniela costina VaduvaDaniela costina Vaduva 1,8975 gold badges20 silver badges22 bronze badges 3
  • This does not looks like valid JSON String – Subir Kumar Sao Commented Apr 24, 2013 at 13:12
  • What's the type of the response ...?? string or JSON...?? – Prasath K Commented Apr 24, 2013 at 13:13
  • using console.log(Object.prototype.toString.call(response)); I receive the following: [object String] – Daniela costina Vaduva Commented Apr 24, 2013 at 13:21
Add a ment  | 

3 Answers 3

Reset to default 3

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

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

2) wrap the whole thing in square brackets:

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

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

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

4) parse the json string:

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)

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/hu3Eu/

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

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..

发布评论

评论列表(0)

  1. 暂无评论