javascript javascript的转义字符串:json中的十六进制

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

Escape string for javascript: hex in json

javascriptjsonhex

提问by mtkachenko

This string

这个字符串

{\x22Address\x22:\x22some address with quotes \x22}

{\x22Address\x22:\x22 带引号的地址 \x22}

is parsed by JSON.parse correctly in browser. Why? What do hex numbers mean in json string? I can't find explanation.

在浏览器中被 JSON.parse 正确解析。为什么?json字符串中的十六进制数字是什么意思?我找不到解释。

回答by Lorenz Meyer

In Javascript a backslash is an escape character. There are several escape sequences, you can find a list here.

在 Javascript 中,反斜杠是转义字符。有几个转义序列,您可以在此处找到列表

The most important:

最重要的:

  • \xfollowed by two hexadecimal characters represent a character by it's ascii code
  • \ufollowed by four hexadecimal characters represent a character by it's unicode number
  • \t, \r, \nyou certainly know already. They are tab, carriage return and new line respectively.
  • \x后跟两个十六进制字符表示一个字符的 ascii 代码
  • \u后跟四个十六进制字符表示一个字符的 unicode 数字
  • \t, \r,\n你肯定已经知道了。它们分别是制表符、回车符和换行符。

回答by eirik

If you look up the hex value 22 in a ascii table, you can see that its the quote sign ( " ). Thats why its parsed correctly. http://www.asciitable.com/

如果您在 ascii 表中查找十六进制值 22,您可以看到它的引号 ( " )。这就是它正确解析的原因。http://www.asciitable.com/

var str= "{\x22test\x22: \x22hello\x22}";
var test = JSON.parse(str);
console.dir(test);

{ test: 'hello' }

{测试:'你好'}

回答by guest271314

Try

尝试

console.log(decodeURIComponent("\x22")); // `"""`

See ascii Chart

ascii 图表