最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Escape string for javascript: hex in json - Stack Overflow

programmeradmin2浏览0评论

This string

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

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

This string

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

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

Share Improve this question edited Aug 12, 2019 at 8:30 Lorenz Meyer 20k23 gold badges83 silver badges128 bronze badges asked Mar 1, 2015 at 16:48 Maxim TkachenkoMaxim Tkachenko 5,80810 gold badges42 silver badges75 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

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

The most important:

  • \x followed by two hexadecimal characters represent a character by it's ascii code
  • \u followed by four hexadecimal characters represent a character by it's unicode number
  • \t, \r, \n you certainly know already. They are tab, carriage return and new line respectively.

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./

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

{ test: 'hello' }

Try

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

See ascii Chart

发布评论

评论列表(0)

  1. 暂无评论