views:

35

answers:

1

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?

+1  A: 

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

Marcel Korpel