简体   繁体   English

在JSON中解析反斜杠转义序列时出错

[英]Error in parsing backslash escape sequence in JSON

I'm trying to parse a JSON string obtained from an API: 我正在尝试解析从API获取的JSON字符串:

 var inputString = '{ "AccountName": "NT AUTHORITY\\\\SYSTEM"}' console.log(JSON.parse(inputString)) // View in browser console to see real error 

Above code gives error: 上面的代码给出错误:

Uncaught SyntaxError: Unexpected token S in JSON at position 31 at JSON.parse () at program.html:7 未捕获的SyntaxError:在program.html的JSON.parse()位置31的JSON中出现意外的标记:7

Now below code works: 现在下面的代码工作:

 var inputString = '{ "AccountName": "NT AUTHORITY\\\\\\\\SYSTEM"}' console.log(JSON.parse(inputString)) 

It shows the output: 它显示了输出:

{AccountName: "NT AUTHORITY\SYSTEM"}

Backslash character is an escape sequence in JSON. 反斜杠字符是JSON中的转义序列。 But why do I require four backslashes to create a single backslash? 但为什么我需要四个反斜杠来创建一个反斜杠? Shouldn't it be only \\\\ ? 不应该只是\\\\

The JSON response that I'm getting from the API being called is giving me only two \\\\ where ever there is a path. 我从被调用的API中获取的JSON响应只给了我两个\\\\ ,其中有一条路径。 So my code is breaking. 所以我的代码破了。 I believe API's JSON format is correct. 我相信API的JSON格式是正确的。 When I try to parse this response on online JSON viewer then they are able to parse it successfully. 当我尝试在在线JSON查看器上解析此响应时,他们能够成功解析它。

First, the template literal is parsed. 首先,解析模板文字。 Just like in ordinary strings, a double backslash is translated into a single literal backslash. 就像在普通字符串中一样,双反斜杠被转换为单个字面反斜杠。 So, your initial inputString : 所以,你的初始inputString

var inputString = `{ "AccountName": "NT AUTHORITY\\SYSTEM"}`

will have a single backslash in it. 将在其一个反斜杠。

 console.log(`\\\\`.length); 

Unlike plain Javascript strings (in which unnecessary backslashes are just ignored), JSON format requires that backslashes precede a special character to be escaped (like a " or another \\ ). If a backslash and the following character does not translate to an escape character, a SyntaxError like the one you see in your question will be thrown. 不同于普通的JavaScript字符串(其中不必要的反斜杠,恰恰忽略了),JSON格式要求反斜杠前面一个特殊的字符转义(如"或其他\\ )。如果一个反斜杠和下面的字符转换为转义字符,将抛出您在问题中看到的SyntaxError

So, to indicate a literal backslash in a JSON, you need two backslashes in the JSON, and to indicate a literal backslash in a Javascript string, you also need two backslashes. 因此,要在JSON中指示文字反斜杠,您需要在JSON中使用两个反斜杠,并且要在Javascript字符串中指示文字反斜杠,您还需要两个反斜杠。 Together, you need four backslashes in order to indicate a literal backslash in the JSON. 在一起,您需要四个反斜杠以指示JSON中的文字反斜杠。

If you wish to manually write strings that include literal backslashes, and you don't need to use escape characters, you might consider using String.raw for your template literals, which parse single backslashes as literal backslashes rather than as part of an escape sequence: 如果您希望手动编写包含文字反斜杠的字符串,并且不需要使用转义字符,则可以考虑使用String.raw作为模板文字,将单个反斜杠解析为文字反斜杠而不是转义序列的一部分:

 console.log( String.raw`\\\\`.length ); const inputString = String.raw`{ "AccountName": "NT AUTHORITY\\\\SYSTEM"}`; console.log(JSON.parse(inputString)) 

You've got string inside template string. 你在模板字符串中有字符串。 One backslash is consumed by template string and the other is for quoted text 模板字符串消耗一个反斜杠,另一个用于引用文本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM