Paul
Paul

Reputation: 26690

Unable to serialize JavaScript object properly

var o = { param1: "value1", param2: "value2" }
console.log(o);
console.log(escape(o));

The first console.log shows Object { param1: "value1", param2: "value2" },

the second: %5Bobject%20Object%5D (i.e. [object Object])

How to serialize an object properly without any plugings?

Upvotes: 1

Views: 367

Answers (1)

VisioN
VisioN

Reputation: 145478

It is easier to use JSON serialization:

var serialized = JSON.stringify(o);
// "{"param1":"value1","param2":"value2"}"

You can read about browser compatibility at MDN: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON#Browser_compatibility

Upvotes: 4

Related Questions