3

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

3
  • This does not looks like valid JSON String Commented Apr 24, 2013 at 13:12
  • What's the type of the response ...?? string or JSON...?? Commented Apr 24, 2013 at 13:13
  • using console.log(Object.prototype.toString.call(response)); I receive the following: [object String] Commented Apr 24, 2013 at 13:21

3 Answers 3

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)

Sign up to request clarification or add additional context in comments.

3 Comments

Who downvoted me by the way? I assume it was somebody who cannot write/type?
Wasn't me, but I don't think this is the way to go either. Instead, OP should fix his PHP script output to generate valid JSON.
@Bergi: Of course, you are right. But I can only answer the question, I would suggest a better way but I don't know enough about how OP server is creating the output data. I also don't do PHP
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.

1 Comment

This is a wonderful example but i have an issue: the response being a string if I use for example $.makeArray(response) I get something like that: [""['2013-02-24', 10]", "['2013-02-25', 17]", "['201…0', 4]", "['2013-04-21', 4]", .... "] which create another issue: how I get rid of those two " ?
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..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.