phantomCoder
phantomCoder

Reputation: 1587

jQuery parseJSON fail

I was doing some stuff with jQuery parseJSON function.

This is the code, which is not working

var obj = $.parseJSON("{'w':'w-1'}");
alert(obj.w);

After debugging for some time I changed the single quotes to double quotes like the code below and it worked fine.

var obj = $.parseJSON('{"w":"wb-001"}');
alert(obj.w);

Some related Questions

1) I just want to know WHY single quotes don't work?

2) Single quotes works fine with eval but not with parseJSON, Why?

var obj = eval("("+"{'w':'w-1'}"+")");
alert(obj.w);

3) I usually write like this

var someString = "HELLO WORLD";

and

var someString = 'HELLO WORLD';

After encountering the above problem I was wondering if I was doing something wrong in all my past javascript coding.

Thanx in advance, kvj

Upvotes: 2

Views: 1964

Answers (1)

alex
alex

Reputation: 490547

1) JSON spec says use double quotes.

String (double-quoted Unicode with backslash escaping)

Source.

2) eval() is not a JSON parser, but a JavaScript evaluator. It will run your string as if it were JavaScript.

3) In JavaScript, they have the same meaning. Just be consistent. I personally use ' because I sometimes deal with serialised HTML and I use " for my attribute values.

Upvotes: 3

Related Questions