YES, i read other posts but i still can ́t figure this out...
I have a multi-dimensional array in php.
json_encode($myarray);
Gives me something like this...
{"1":[0,0.46,0.23],"2":[0,0.71,0.33],"3":[0,0.7,0.54]}
Yes, maaaany floaties. I love them ;). How can i parse it with jQuery? I tried
var myarray = $.parseJSON(<?php echo json_encode($myarray); ?>);
alert(myarray[0][0].val());
but it doesn ́t work :/. Maybe i ́m just to dumb right now.
Thanks for any suggestions!
2 Answers 2
var myArray = <?php echo json_encode($myarray); ?>
Since JSON is Javascript, there is no real parsing from one data type to another. JSON is a subset of javascript, its just compatible. Especially jQuery doesnt have anything to do with this.
If the data comes from an unknown source (e.g. the user) you'd want to validate it first.
Comments
why would you parse it?
var json = <?php echo json_encode($array); ?>
parseJSON()is for strings that contain a JSON value. JSON itself however is legal JavaScript, no need to wrap it in a string in this case.