If I'm converting a simple JavaScript object to a string, all special chars will be converted to hex code.
function O() {
this.name = "<üäö!";
}
var myObject = new O();
console.log(myObject.toSource());
Result:
{name:"<\xFC\xE4\xF6!"}
How would I avoid this or convert all hex chars back to utf8 chars?
If you use Crockford's json2.js, you completely avoid this issue.
console.log(JSON.stringify(myObject));
outputs
{"name":"<üäö!"}
You can then send this string, e.g. using an XMLHttpRequest (in that case, don't forget to use encodeURIComponent).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With