Reputation: 38180
It is said here:
any quotes inside the JSON have to be “escaped” with a backslash in front. Otherwise, JavaScript gets confused about which quotes we want for display and which quotes are part of the programming.
but in their code snippet I can't see any escaping character is this tutorial buggy I'm confused ? :
var movielisttext = "{"movielist": ["Friday the 13th", "Friday the 13th Part 2", "Friday the 13th Part III", "Friday the 13th: The Final Chapter", "Friday the 13th: A New Beginning"]}";
My question is specifically if their article has an error or not because it amazes me that a tutorial for beginner can embed such error.
Upvotes: 2
Views: 17833
Reputation: 943209
What you have is JavaScript, not JSON.
If you want JSON:
{
"movielist": [
"Friday the 13th",
"Friday the 13th Part 2",
"Friday the 13th Part III",
"Friday the 13th: The Final Chapter",
"Friday the 13th: A New Beginning"
]
}
If you want a JavaScript object
var movielisttext = {
"movielist": [
"Friday the 13th",
"Friday the 13th Part 2",
"Friday the 13th Part III",
"Friday the 13th: The Final Chapter",
"Friday the 13th: A New Beginning"
]
};
If you want a JavaScript string containing the JSON:
var movielisttext = '{"movielist": ["Friday the 13th","Friday the 13th Part 2","Friday the 13th Part III","Friday the 13th: The Final Chapter","Friday the 13th: A New Beginning"]}';
or
var movielisttext = "{\"movielist\": [\"Friday the 13th\",\"Friday the 13th Part 2\",\"Friday the 13th Part III\",\"Friday the 13th: The Final Chapter\",\"Friday the 13th: A New Beginning\"]}";
Since the data itself doesn't include any "
characters, they don't need to be escaped as far as the JSON is concerned.
Upvotes: 4
Reputation: 900
var movielisttext = '{"movielist": ["Friday the 13th", "Friday the 13th Part 2", "Friday the 13th Part III", "Friday the 13th: The Final Chapter", "Friday the 13th: A New Beginning"]}';
OR
var movielisttext = "{\"movielist\": [\"Friday the 13th\", \"Friday the 13th Part 2\", \"Friday the 13th Part III\", \"Friday the 13th: The Final Chapter\", \"Friday the 13th: A New Beginning\"]}";
Would do the trick.
Upvotes: 1
Reputation: 22438
Strings in JSON must always be wrapped in double quotes. In that example they should have formatted the JSON like this:
var movielisttext = '{"movielist": ["Friday the 13th", "Friday the 13th Part 2", "Friday the 13th Part III", "Friday the 13th: The Final Chapter", "Friday the 13th: A New Beginning"]}';
But if their intention was to create a literal Javascript object, they should have used:
var movielisttext = {"movielist": ["Friday the 13th", "Friday the 13th Part 2", "Friday the 13th Part III", "Friday the 13th: The Final Chapter", "Friday the 13th: A New Beginning"]};
In the first case, the value of movielisttext
is a string, in the second case it's an object
Upvotes: 1
Reputation: 655209
Since this is JavaScript and not JSON, just omit the surrounding quotes:
var movielisttext = {"movielist": ["Friday the 13th", "Friday the 13th Part 2", "Friday the 13th Part III", "Friday the 13th: The Final Chapter", "Friday the 13th: A New Beginning"]};
Upvotes: 2