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
Something like this. Is that trailing comma intentional?
var getDayArray = JSON.parse(data).day0.split(",")
Most modern browsers have support for JSON.parse(). You would use it thusly:
var dataJSON = '{"fname":"Todd","lname":"James","cascade":"tjames","loc":"res","place":"home", "day0":"0,1,2,3"}'; // You need to remove the trailing comma
var data = JSON.parse(dataJSON);
var getDay = data.day0;
var getDayArray = getDay.split(",");
However, it might be better to modify whatever is generating the value for dataJSON, to return
var dataJSON = '{"fname":"Todd","lname":"James","cascade":"tjames","loc":"res","place":"home", "day0":[0,1,2,3]}';
This is built into most modern browser JavaScript engines. Depending on what environment you are targeting you can simply do:
var data = JSON.parse(jsonString);
day0 = data.day0.split(",");
It's pretty simple. If you are targeting environments that don't have access to a built in JSON object you should try this JSON project.