javascript 的转义字符串:json 中的十六进制
Escape string for javascript: hex in json
这个字符串
{\x22Address\x22:\x22some address with quotes \x22}
在浏览器中被 JSON.parse 正确解析。为什么? json 字符串中的十六进制数字是什么意思?我找不到解释。
尝试
console.log(decodeURIComponent("\x22")); // `"`
如果您在 ascii table 中查找十六进制值 22,您可以看到它是引号 (")。这就是它被正确解析的原因。http://www.asciitable.com/
var str= "{\x22test\x22: \x22hello\x22}";
var test = JSON.parse(str);
console.dir(test);
{ 测试:'hello' }
在 Javascript 中,反斜杠是转义字符。有几个转义序列,可以find a list here.
最重要的:
\x
后跟两个十六进制字符用ascii码表示一个字符
\u
后接四个十六进制字符表示一个字符的 unicode 编号
\t
, \r
, \n
你肯定已经知道了。它们分别是制表符、回车return和换行
这个字符串
{\x22Address\x22:\x22some address with quotes \x22}
在浏览器中被 JSON.parse 正确解析。为什么? json 字符串中的十六进制数字是什么意思?我找不到解释。
尝试
console.log(decodeURIComponent("\x22")); // `"`
如果您在 ascii table 中查找十六进制值 22,您可以看到它是引号 (")。这就是它被正确解析的原因。http://www.asciitable.com/
var str= "{\x22test\x22: \x22hello\x22}";
var test = JSON.parse(str);
console.dir(test);
{ 测试:'hello' }
在 Javascript 中,反斜杠是转义字符。有几个转义序列,可以find a list here.
最重要的:
\x
后跟两个十六进制字符用ascii码表示一个字符\u
后接四个十六进制字符表示一个字符的 unicode 编号\t
,\r
,\n
你肯定已经知道了。它们分别是制表符、回车return和换行