问题描述
下面的JSON格式示例包含一个反斜杠,如果我运行 JSON.load
,反斜杠就会消失:
The following example in JSON format contains one backslash, and if I run JSON.load
, the backslash disappears:
JSON.load('{ "88694": { "regex": ".*?\. (CVE-2015-46055)" } }')
# => {"88694"=>{ "regex"=>".*?. (CVE-2015-46055)"}}
如何保留反斜杠?
我的目标是拥有这种结构,并在需要时读取文件并加载JSON
My goal is to have this structure, and whenever I need, read the file, load the JSON into Hash, and search for those regular expressions.
UPDATE 1
这是我想要的一个示例。
here is an example what I want.
irb> "stack.overflow"[/.*?\./]
=> "stack."
我无法将正则表达式从JSON顺序传递给我的字符串赶上 。,因为 \。
I can't pass the regex from JSON to my string in order to catch that ".", because the "\." disappears.
推荐答案
str = '{ "88694": { "regex": ".*?\. (CVE-2015-46055)" } }'
#=> "{ \"88694\": { \"regex\": \".*?\\. (CVE-2015-46055)\" } }"
str.chars
#=> ["{", " ", "\"", "8", "8", "6", "9", "4", "\"", ":", " ", "{", " ",
# "\"", "r", "e", "g", "e", "x", "\"", ":", " ", "\"", ".", "*", "?",
# "\\", ".",
# ~~~ ~~
# " ", "(",..., "}", " ", "}"]
我们发现 str
确实包含反斜杠字符和句点,原因是 str
用单引号引起来。 \。
仅在 str
用双引号引起来时才视为转义期:
This shows us that str
does indeed contain a backslash character followed by a period. The reason is that str
is enclosed in single quotes. \.
would only be treated as an escaped period if str
were enclosed in double quotes:
"{ '88694': { 'regex': '.*?\. (CVE-2015-46055)' } }".chars[25,3]
#=> ["?", ".", " "]
str
的返回值将单引号字符串转换为双引号字符串:
The return value of str
converts the single-quoted string to a double-quoted string:
"{ \"88694\": { \"regex\": \".*?\\. (CVE-2015-46055)\" } }"
\\
是一个反斜杠字符后跟一个句点。现在可以使用双引号将句号转义,但是它前面不能带有反斜杠,而只能带有一个退格字符。
\\
is one backslash character followed by a period. With the double quotes the period can now be escaped, but it is not preceded by a backslash, only by a backspace character.
现在让我们添加另一个反斜杠,看看会发生什么:
Now let's add another backslash and see what happens:
str1 = '{ "88694": { "regex": ".*?\\. (CVE-2015-46055)" } }'
str1.chars == str.chars
#=> true
结果相同。那是因为单引号支持转义序列 \\
(单反斜杠)(并且只有一个: \'
[单引号])。
The result is the same. That is because single quotes support the escape sequence \\
(single backslash) (and only one other: \'
[single quote]).
现在让我们添加第三个反斜杠:
Now let's add a third backslash:
str2 = '{ "88694": { "regex": ".*?\\\. (CVE-2015-46055)" } }'
str2.chars
#=> ["{", " ", "\"", "8", "8", "6", "9", "4", "\"", ":", " ", "{", " ",
# "\"", "r", "e", "g", "e", "x", "\"", ":", " ", "\"", ".", "*", "?",
# "\\", "\\", ".",
# ~~~~ ~~~~ ~~~
# " ", "(",..., "}", " ", "}"]
是否惊讶? \\
产生一个反斜杠字符(单引号中的转义反斜杠), \
产生第二个反斜杠字符(单引号中的反斜杠)和。
是单引号中的句点。
Surprised? \\
produces one backslash character (escaped backslash in single quotes), \
products a second backslash character (backslash in single quotes) and .
is a period in single quotes.
我们获得:
s = {"88694"=>{"regex"=>".*?\\. (CVE-2015-46055)"}.to_json
JSON.parse(str)
#=> {"88694"=>{"regex"=>".*?. (CVE-2015-46055)"}}
JSON.parse(str1)
#=> {"88694"=>{"regex"=>".*?. (CVE-2015-46055)"}}
JSON.parse(str2)
#=> {"88694"=>{"regex"=>".*?\\. (CVE-2015-46055)"}}
str2
是我们想要的,因为
JSON.parse(str2)["88694"]["regex"].chars[2,4]
#=> ["?", "\\", ".", " "]
我们可以或者向后工作:
We could alternatively work backwards:
js = {"88694"=>{"regex"=>".*?\\. (CVE-2015-46055)"}}.to_json
#=> "{\"88694\":{\"regex\":\".*?\\\\. (CVE-2015-46055)\"}}"
'{"88694":{"regex":".*?\\\. (CVE-2015-46055)"}}' == js
#=> true
此字符串与 str2
相同
似乎JSON将两个连续的反斜杠字符视为一个反斜杠字符。请参阅@Jordan的评论。
It appears that JSON treats two successive backslash characters as one backslash character. See @Jordan's comment.
也许读者可以在这里详细说明JSON在做什么。
Perhaps a reader can elaborate what JSON is doing here.
这篇关于为什么ruby的JSON解析器会吃掉我的反斜杠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!