javascript 解析 JSON 时导致此语法错误的原因是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7717549/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 01:03:13  来源:igfitidea点击:

What is causing this syntax error while parsing JSON?

javascriptjson

提问by soldier.moth

I have a string in javascript:

我在 javascript 中有一个字符串:

var test = '{"test":"\-"}'

When I try to parse this as JSON with the following:

当我尝试使用以下内容将其解析为 JSON 时:

JSON.parse(test)

or with:

或与:

$.parseJSON(test)

I get a SyntaxError of type "unexpected_token_number". This the value for the "test"attribute is a user enterable field. How should I properly escape this field?

我收到类型为“unexpected_token_number”的 SyntaxError。该"test"属性的值是用户可输入的字段。我应该如何正确逃离这个领域?

回答by Felix Kling

'{"test":"\\-"}'will be interpreted as JavaScript string, with the result being {"test":"\-"}.

'{"test":"\\-"}'将被解释为 JavaScript 字符串,结果为{"test":"\-"}.

As you can see in these diagrams, \-is not a valid escape sequence (valid are \", \\, \/, \b, \f, \n, \r, \t, \uxxxx).

正如您在这些图中所看到的,\-不是有效的转义序列(有效是\"\\\/\b\f\n\r\t\uxxxx)。

JSONLintalso gives the error

JSONLint也给出了错误

Parse error on line 2:
{    "test": "\-"}
-------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['

If you want two backslashes in the JSON you have to escape them in the JavaScript string as well, so you'd end up with

如果你想在 JSON 中使用两个反斜杠,你也必须在 JavaScript 字符串中转义它们,所以你最终会得到

var test = '{"test": "\\-"}';

Otherwise, omit it:

否则,省略它:

var test = '{"test": "-"}';

回答by Quentin

Your JSON looks like this:

您的 JSON 如下所示:

{
    "test": "\-"
}

Inside a string, a \character must be followed by", \, /, b, f, n, r, tor u????where the question marks represent hexadecimal digits.

在字符串中,\字符后面必须跟有", \, /, b, f, n, r,t或者u????问号代表十六进制数字的地方。

You probably want the JSON to look like this:

您可能希望 JSON 如下所示:

{
    "test": "\-"
}

And are failing to convert it to a JavaScript string properly, but not escaping the \characters for JavaScript.

并且未能将其正确转换为 JavaScript 字符串,但没有转义\JavaScript的字符。

var test = '{"test":"\\-"}'

回答by Deleteman

Here you go:

干得好:

var test = '{"test":"\-"}'
JSON.parse(test)

Is that what you wanted? To have: {test: "-"}?

那是你想要的吗?有:{test: "-"}?

Otherwiste:

其他:

var test = '{"test":"\\-"}'
JSON.parse(test)

To have: {test:"\-"}

具有: {test:"\-"}

Cheers

干杯

回答by James Hill

What do you want the value of test to be? The \is an escape character in JavaScript. Assuming that you're trying to escape it and that you want the value of test to be \-, this will work:

你希望 test 的值是多少?该\是在JavaScript中的转义字符。假设您试图逃避它并且您希望 test 的值为\-,这将起作用:

var test = '{"test":"\\-"}'

//Result: { test="\-"}

回答by Guffa

When I try to parse that string, I get "bad escaped character".

当我尝试解析该字符串时,我得到“错误的转义字符”。

The reason is that you are escaping a character that doesn't need escaping. The \\in the string literal is an escaped backslash, so the string contains the escape code \-. A dash doesn't need escaping, so that escape code is not allowed by the JSON parser.

原因是你在逃避一个不需要逃避的角色。将\\在字符串文字是一个转义反斜线,所以字符串中包含换码\-。破折号不需要转义,因此 JSON 解析器不允许转义代码。

If you want to put an escaped backslash in the JSON you have to escape it to put it in a literal string:

如果要在 JSON 中放置转义反斜杠,则必须将其转义以将其放入文字字符串中:

var test = '{"test":"\\-"}'