When I JSON.stringify() the following code:
var exampleObject = { "name" : "Žiga Kovač", "kraj" : "Žužemberk"};
I get different results between browsers.
IE8 and Google Chrome return:
{"name":"\u017diga Kova\u010d","kraj":"\u017du\u017eemberk"}
While Firefox and Opera return:
{"name":"Žiga Kovač","kraj":"Žužemberk"}
I am using the browser’s native JSON implementation in all 4 browsers. If I undefine the native JSON implementation and replace it with the one from json.org, then all browsers return:
{"name":"Žiga Kovač","kraj":"Žužemberk"}
Why is this happening, which result is correct and is it possible to make that all browsers return:
{"name":"\u017diga Kova\u010d","kraj":"\u017du\u017eemberk"}
?
These two representations are absolutely equivalent.
The one uses Unicode escape sequences (
\uxxxx) to represent a Unicode character, the other uses an actual Unicode character. json.org defines a string as:string - "" - "chars" chars - char - char chars char - any Unicode character except " or \ or control characters - one of: \" \\ \/ \b \f \n \r \t - \u four-hex-digitsThere is no difference in the strings themselves, only in their representation. This is the same thing HTML does when you use
©,©or©to represent the copyright sign.