Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare a json string correctly?

So I tested two ways of declaring a json string:

1:

json = "{'name': 'ajsie'}";
obj = JSON.parse(json); // SyntaxError: Unexpected token ILLEGAL

2:

json = '{"name": "ajsie"}';
obj = JSON.parse(json); // Worked!

What is the problem with the first one?

like image 369
ajsie Avatar asked Oct 21 '10 22:10

ajsie


2 Answers

Single quotes are not a valid quote character for strings. From http://www.json.org/: "A value can be a string in double quotes..."

like image 71
SamStephens Avatar answered Oct 04 '22 14:10

SamStephens


json.org defines a string to use " instead of '. That's my guess.

like image 39
Anon Avatar answered Oct 04 '22 15:10

Anon