0

I have this data coming from a REST method using jquery's getJSON method.

"[Date.UTC(2010,0,0,0,0,0,0), 157],[Date.UTC(2010,0,0,0,0,420,1), 157],[Date.UTC(2010,0,0,0,0,420,2), 282],[Date.UTC(2010,0,0,0,0,600,3), 282],[Date.UTC(2010,0,0,0,0,600,4), 125],[Date.UTC(2010,0,0,0,0,900,5), 125],[Date.UTC(2010,0,0,0,0,900,6), 282],[Date.UTC(2010,0,0,0,0,2100,7), 282],[Date.UTC(2010,0,0,0,0,2100,8), 125],[Date.UTC(2010,0,0,0,0,2400,9), 125],[Date.UTC(2010,0,0,0,0,2400,10), 295],[Date.UTC(2010,0,0,0,0,3600,11), 295],[Date.UTC(2010,0,0,0,0,3600,12), 125],[Date.UTC(2010,0,0,0,0,3900,13), 125],[Date.UTC(2010,0,0,0,0,3900,14), 288],[Date.UTC(2010,0,0,0,0,5100,15), 288],[Date.UTC(2010,0,0,0,0,5100,16), 125],[Date.UTC(2010,0,0,0,0,5400,17), 125]"

It comes back as a string. I need to parse it into a two dimensional array. Each item in the array should have a date and a value.

I also have full control over the REST method, so I could change the way the data returns. I'm interested in making this as fast as possible.

Here's what we are doing now which I think could be improved:

 var jqxhr = $.getJSON(getDataURL, function(dataResult) {
        var result = dataResult;

        result =result.replace(/\]\,\[/g, ']:[');
        result = result.replace(/\)\,/g, ');');

        var tempArray = result.split(':');

        var myarray = new Array();
        myarray[0] = new Array(2); // Make the first element an array of two elements
        for(i = 0; i < tempArray.length; i ++)
        {
          myarray[i] = tempArray[i].split(';');
          myarray[i][1] = myarray[i][1].replace(/\"/g,'');
          myarray[i][1] = myarray[i][1].replace(/\]/g,'');  
          myarray[i][0] = myarray[i][0].replace(/\[/g,'');                             
        }                                                               
    })
Nate
  • 2,316
  • 4
  • 35
  • 53
  • as horrible as it sounds, I'd put brackets at the start and end of the string and use eval – Joseph Marikle Aug 22 '11 at 21:49
  • @Joseph I thought about using jQuery.parseJSON but I don't think my string is valid for that function. http://api.jquery.com/jQuery.parseJSON/ I'll try sending back the data so it's valid. – Nate Aug 22 '11 at 21:54

2 Answers2

3

As much as I'm going to get flack for this (eval tends to be a security risk), I would just do

var myarray = eval("[" + result + "]");
Joseph Marikle
  • 76,418
  • 17
  • 112
  • 129
  • 1
    I was going to post the same answer. In this case, `eval` works (you don't even need to add the enclosing brackets). But one should know what he is doing before using `eval`. Could start by reading this SO thread: http://stackoverflow.com/questions/197769/when-is-javascripts-eval-not-evil – bfavaretto Aug 22 '11 at 21:56
  • @bfavaretto Wow! thanks for the link! I will be sure to read it when I get home. Also, I tried it without the brackets and it only assigned it the last array in the string so I think the brackets are necessary. Again, thank you for the link! – Joseph Marikle Aug 22 '11 at 22:01
  • You're right about the brackets, sorry! I tested with only part of the original string... – bfavaretto Aug 22 '11 at 22:05
  • *eval* is not, of itself, a secuirity risk. Running code from unknown sources without checking might be, but you don't need *eval* for that. – RobG Aug 22 '11 at 23:34
0

You should use JSON to return data from your server to your JS script. That will be pretty easier to manipulate it as an array.

ldiqual
  • 15,015
  • 6
  • 52
  • 90