javascript json字符串中的转义字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4113967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Escaping character in json string
提问by user310291
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.
JSON 中的任何引号都必须在前面用反斜杠“转义”。否则,JavaScript 会混淆我们想要显示哪些引号以及哪些引号是编程的一部分。
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.
我的问题特别是他们的文章是否有错误,因为让我惊讶的是初学者教程可以嵌入这样的错误。
回答by Quentin
What you have is JavaScript, not JSON.
你拥有的是 JavaScript,而不是 JSON。
If you want JSON:
如果你想要 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
如果你想要一个 JavaScript 对象
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:
如果你想要一个包含 JSON 的 JavaScript 字符串:
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.
由于数据本身不包含任何"字符,因此就 JSON 而言,它们不需要进行转义。
回答by Gumbo
Since this is JavaScript and not JSON, just omit the surrounding quotes:
由于这是 JavaScript 而不是 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"]};
回答by Harmen
Strings in JSON must always be wrapped in double quotes. In that example they should have formatted the JSON like this:
JSON 中的字符串必须始终用双引号括起来。在那个例子中,他们应该像这样格式化 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"]}';
But if their intention was to create a literal Javascript object, they should have used:
但是如果他们的意图是创建一个文字 Javascript 对象,他们应该使用:
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 movielisttextis a string, in the second case it's an object
在第一种情况下,的值movielisttext是一个字符串,在第二种情况下它是一个对象
回答by partoa
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.
会做的伎俩。

