Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON.parse throws error

<!DOCTYPE html>

<html>
<head>
    <title></title>
</head>
<body>
    <script>        
        var str = "{ 'foo': 'bar' }";
        var json = JSON.parse(str);
    </script>   
</body>
</html>

This code throws an error on the second variable statement. Why? (Chrome says "unexpected token ILLEGAL", Firefox says "JSON.parse")

like image 991
Šime Vidas Avatar asked Nov 13 '10 16:11

Šime Vidas


1 Answers

You're supposed to use double, not single quotes:

 var str = '{ "foo": "bar" }';
 var json = JSON.parse(str); 
 json['foo']
like image 155
meder omuraliev Avatar answered Sep 28 '22 01:09

meder omuraliev