jQuery serialize and unserialize

.serialize() will map your input controls that have a name attribute defined into a standard query string:

foo=bar&bar=foo&and=soon

That kind of string is easy accesible in almost every "backend" programming language.

If you need to serialize object information, use JSON.

var obj = {
    foo:  'bar',
    more: 'etc
};

serialize this with window.JSON.stringify(obj);. To unserialize such a JSON string, use window.JSON.parse(str);, which returns a javascript object.

Many languages support this principle.


jQuery has a serialize function.

$("#form").serialize(); // Returns serialized string

Reference: http://api.jquery.com/serialize/


If you want unserialize you must create a function like this;

function unserialize(data) {
    data = data.split('&');
    var response = {};
    for (var k in data){
        var newData = data[k].split('=');
        response[newData[0]] = newData[1];
    }
    return response;
}

this function inverse serialize() and return a json data.

Tags:

Jquery