Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse JSON string into an array

I'm trying to parse a string from JSON and turn those elements into an array in Javascript. Here's the code.

      var data = "{"fname":"Todd","lname":"James","cascade":"tjames","loc":"res","place":"home", "day0":"0,1,2,3,"}";
      var getDay = data.day0;
      var getDayArray = getDay.split(",");

Essentially, I'm trying to get day0, which is 0,1,2,3, and turn it into an array with a structure of

[0] = 0
[1] = 1
[2] = 2
[3] = 3

What is the best way to go about doing this?

like image 736
Vikram Avatar asked Jul 12 '12 21:07

Vikram


1 Answers

Something like this. Is that trailing comma intentional?

var getDayArray = JSON.parse(data).day0.split(",")
like image 147
Mike Robinson Avatar answered Nov 14 '22 23:11

Mike Robinson