Skip to content Skip to sidebar Skip to footer

Escape String For Javascript: Hex In Json

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 explanat

Solution 1:

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.

Solution 2:

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/

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

{ test: 'hello' }


Solution 3:

Try

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

See ascii Chart


Post a Comment for "Escape String For Javascript: Hex In Json"