[ACCEPTED]-jQuery serialize an object?-serialization

Accepted answer
Score: 41

You should use jQuery.param() instead.

Working Example

With vanilla JS, you 1 would use JSON.stringify instead.

Score: 7

As mentioned you should use .param()

$.param({id: 1, name: "Some name", color: '#444444' })

But 3 also you need to be careful with your syntax. Your 2 brackets don't match, and that color will 1 need quotation marks. jsFiddle

Score: 4

You could use JSON.stringify to serialize your object, and 1 you'd have to wrap your color string correctly:

var obj = {id: 1, name: "Some name", color: '#444444' };
var serialized = JSON.stringify(obj);
// => "{"id":1,"name":"Some name","color":"#444444"}"

More Related questions