Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery.parseJSON single quote vs double quote

What actually the difference between this?

This works fine:

var obj1 = jQuery.parseJSON('{"orderedList": "true"}'); document.write("obj1 "+ obj1.orderedList ); 

but the following does not work:

var obj2 = jQuery.parseJSON("{'orderedList': 'true'}"); document.write("obj2 "+ obj2.orderedList ); 

Why is that?

like image 854
sadaf2605 Avatar asked Jan 16 '13 09:01

sadaf2605


People also ask

Can I use single quotes in JSON?

Strings in JSON are specified using double quotes, i.e., " . If the strings are enclosed using single quotes, then the JSON is an invalid JSON .

Should I use single or double quotes in JSON?

Answer #1:JSON requires double quotes for its strings.

How does JSON value handle double quotes?

if you want to escape double quote in JSON use \\ to escape it.


1 Answers

That's because double quotes is considered standard while single quote is not. This is not really specific to JQuery, but its about JSON standard. So irrespective of JS toolkit, you should expect same behaviour.

A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.

Update

Or perhaps its a duplicate of jQuery single quote in JSON response

like image 148
ch4nd4n Avatar answered Nov 07 '22 13:11

ch4nd4n