javascript - JSON.stringify - Strange behaviour with backslash in array -
i playing js while noticed strange behaviour backslash \
inserted string within array printed using json.stringify()
. of course backslash used escaping special chars, happens if need put backslash in string? use backslash escape you're thinking, doesn't work json.stringify
this should print 1 backslash
array = ['\\']; document.write(json.stringify(array));
this should print 2 backslashes
array = ['\\\\']; document.write(json.stringify(array));
am missing something? considered bug of json.stringify?
it correct. json.stringify
return required string recreate object - string requires escape backslash, return required escape backslash generate string properly.
try this:
array = ['\\']; var x = json.stringify(array) var y = json.parse(x) if (array[0] == y[0]) alert("it works")
or
array = ['\\']; if (json.parse(json.stringify(array))[0] == array[0]) alert("it works")
Comments
Post a Comment