Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery deserialize form

I am using jQuery Serialize to serialize my form elements and would like to deserialize them back. Unfortunately can't find any working jQuery deserializer, any suggestions?

like image 842
Tomas Avatar asked Aug 09 '11 07:08

Tomas


People also ask

What is serialize vs deserialize?

Serialization is a mechanism of converting the state of an object into a byte stream. Deserialization is the reverse process where the byte stream is used to recreate the actual Java object in memory. This mechanism is used to persist the object.

What is form serialize ()?

The serialize() method creates a URL encoded text string by serializing form values. You can select one or more form elements (like input and/or text area), or the form element itself. The serialized values can be used in the URL query string when making an AJAX request.

How do you serialize form data?

To serialize a FormData object into a query string, pass it into the new URLSearchParams() constructor. This will create a URLSearchParams object of encoded query string values. Then, call the URLSearchParams. toString() method on it to convert it into a query string.

How do you serialize data in JavaScript?

In JavaScript, for example, you can serialize an object to a JSON string by calling the function JSON. stringify() . CSS values are serialized by calling the function CSSStyleDeclaration. getPropertyValue() .


2 Answers

I wrote a version of jQuery.deserialize that supports serialized data generated from the serialize, serializeArray and serializeObject functions. It also supports all form element types, including checkboxes and radio buttons.

like image 187
kflorence Avatar answered Sep 18 '22 16:09

kflorence


Try this:

function deparam(query) {     var pairs, i, keyValuePair, key, value, map = {};     // remove leading question mark if its there     if (query.slice(0, 1) === '?') {         query = query.slice(1);     }     if (query !== '') {         pairs = query.split('&');         for (i = 0; i < pairs.length; i += 1) {             keyValuePair = pairs[i].split('=');             key = decodeURIComponent(keyValuePair[0]);             value = (keyValuePair.length > 1) ? decodeURIComponent(keyValuePair[1]) : undefined;             map[key] = value;         }     }     return map; } 
like image 29
Jack Allan Avatar answered Sep 19 '22 16:09

Jack Allan