Is there a difference in the JSON Key when using a single quote and a double quote?

I ran two parts of javascript codes in the JS online platform: Link to the site

pets = '{'pet_names':[{"name":"jack"},{"name":"john"},{"name":"joe"}]}';
var arr = JSON.parse(pets);
alert(arr.pet_names[1].name);

Code with double quotes ("pet_names") will be fine, but with single quotes ("pet_names") will remind you of the error: "Unexpected identifier"

pets = '{"pet_names":[{"name":"jack"},{"name":"john"},{"name":"joe"}]}';
var arr = JSON.parse(pets);
alert(arr.pet_names[1].name);

So why is this going to happen?

+4
source share
2 answers

In JSON, only double quotes are valid.

You can find the standard at JSON.org

​​ , , false null, . .

, .

+9

, ,
, ,

pets = '{\'pet_names\':[{"name":"jack"},{"name":"john"},{"name":"joe"}]}';

, .

, json, (SyntaxError: Unexpected token), JSON , , JavaScript, JSON.

+1

Source: https://habr.com/ru/post/1547291/


All Articles