开发者

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 开发者_如何学JAVAadvance, kvj


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜