As I stated above, I have a PHP script and a JavaScript, I have a few objects that read text files on the server side and then pass the data to the JavaScript.
Here's my entire code:
map.html: http://www.pastebin.com/b45mbvgp and index.php: http://www.pastebin.com/zibdquzu
The part that really matters:
var x = <?php echo json_encode($streetsObject); ?>;
var obj = eval("("x")");
I've also tried
var obj = JSON.parse(x);
the X variable does get set to the size of the passed object, 527 (tested it) but when I try to use the eval or JSON parse function is simply doesn't work. Do I have some sort of mistake in my html code which is messing with my calls to other libraries? If so that would be weird because kinetic.js is working just fine. I've been googling examples of JSON and I have yet to see an example of parsing a passed object, it's all examples of local objects :(
(Code works fine if I remove eval / json line of code)
Simply do:
var obj = <?php echo json_encode($streetsObject); ?>;
JSON means JavaScript Object Notation. If you insert JSON directly to Javascript, it will run fine, just in this case. No parsing needed. eval
is not recommended to use for JSON parsing, but the same applies to that (note that eval
actually works because JSON is valid Javascript!).
JSON.parse
is only needed if you have JSON in a Javascript string. So this would work:
var str = '<?php echo json_encode($streetsObject); ?>';
var obj = JSON.parse(str);