[英]Double backslash on quotes when JSON stringifying an array of strings
I'm trying to stringify a JSON object that contains a string with quotes inside of it: 我正在尝试对其中包含带引号的字符串的JSON对象进行字符串化:
array = ['bar "foo"']
However, the string is created as: '["bar \\\\"foo\\\\""]'
when I was hoping for something more along the lines of '["bar \\"foo\\""]'. 但是,当我希望沿着'[“ bar \\” foo \\“”]'的含义获取更多信息时,字符串创建为:
'["bar \\\\"foo\\\\""]'
“”]'。 Why are there two backslashes generated? 为什么会生成两个反斜杠? Thanks
谢谢
Why are there two backslashes generated?
为什么会生成两个反斜杠?
Because backslashes must be escaped by backslashes to represent one single backslash in a string literal. 因为必须使用反斜杠对反斜杠进行转义,以表示字符串文字中的单个反斜杠。
The string 字符串
'["bar \\"foo\\""]'
// or
"[\"bar \\\"foo\\\"\"]"
represents the value 代表价值
["bar \"foo\""]
which is JSON for an array object containing the string value bar "foo"
. 这是包含字符串值
bar "foo"
的数组对象的JSON。
Probably the confusion was caused when you expected to see the value but the tool you used for that printed the string literal. 当您希望看到该值但您使用的用于打印字符串文字的工具时,可能引起了混淆。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.