views:

98

answers:

5

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?

+12  A: 

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

SamStephens
+2  A: 

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

Anon
+2  A: 

Check http://www.json.org/

Strings in JSON object must be enclosed in double quotes.

ArtBIT
Okay, when I run 'asd' in Chrome it automatically converts it to "asd". Therefore I thought that was okay.
weng
Yeah, it's a tricky error @weng. One would think that single and double quotes are interchangeable as everywhere else.
ArtBIT
+1  A: 

http://www.json.org/ is a great reference for JSON. Apparently you have to use double quotes for strings in JSON. I learned something new today too. :)

shoebox639
A: 

{ 'key' : 'val' } is not properly formatted json.

chovy