Serializing to JSON in jQuery
JSON-js - JSON in JavaScript.
To convert an object to a string, use JSON.stringify
:
var json_text = JSON.stringify(your_object, null, 2);
To convert a JSON string to object, use JSON.parse
:
var your_object = JSON.parse(json_text);
It was recently recommended by John Resig:
...PLEASE start migrating
your JSON-using applications over to
Crockford's json2.js. It is fully
compatible with the ECMAScript 5
specification and gracefully degrades
if a native (faster!) implementation
exists.In fact, I just landed a change in jQuery yesterday that utilizes the
JSON.parse method if it exists, now
that it has been completely specified.
I tend to trust what he says on JavaScript matters :)
All modern browsers (and many older ones which aren't ancient) support the JSON object natively. The current version of Crockford's JSON library will only define JSON.stringify
and JSON.parse
if they're not already defined, leaving any browser native implementation intact.
jquery how to deserialize json object
You can do this with:
var mydata; // [{"id":"67","name":"TestString"}]
var json = $.parseJSON(mydata);
the json variable will contain the de-serialized json object
How to add json object with $(form).serialize()?
Serialize() will return a query string so use,
formData+='&test=test';
Code,
var formData = $(form).serialize(); // its a string
formData+='&test=test'; // append in string
$.ajax({
url : '',
type : 'post',
dataType : 'json',
data : formData,
.....
And to append value in an object use
formData['test']='test';
Code,
var formData = {'value1' : 'one','value2' : 'two'}; // its an object
formData['test']='test';
$.ajax({
url : '',
type : 'post',
dataType : 'json',
data : formData,
.....
Converting serialized form's data to json object
Change your statement
var formData = JSON.parse($("#floorplan-form").serializeArray());
with
var formData = JSON.stringify(jQuery('#frm').serializeArray()); // store json string
or
var formData = JSON.parse(JSON.stringify(jQuery('#frm').serializeArray())) // store json object
Serialize complex form to JSON object using jQuery
Try this code I wrote for you... Works fine for me, just using your data result. You can work on it and make a simple jQuery plugin...
The sample need JSON.stringify to work fully.
var d = {
'name': 'name value',
'phone[0][type]': 'cell',
'phone[0][number]': '000',
'phone[1][type]': 'home',
'phone[1][number]': '111',
};
$(document).ready(function(){
arrangeJson(d);
alert(JSON.stringify(d));
});
function arrangeJson(data){
var initMatch = /^([a-z0-9]+?)\[/i;
var first = /^\[[a-z0-9]+?\]/i;
var isNumber = /^[0-9]$/;
var bracers = /[\[\]]/g;
var splitter = /\]\[|\[|\]/g;
for(var key in data) {
if(initMatch.test(key)){
data[key.replace(initMatch,'[$1][')] = data[key];
}
else{
data[key.replace(/^(.+)$/,'[$1]')] = data[key];
}
delete data[key];
}
for (var key in data) {
processExpression(data, key, data[key]);
delete data[key];
}
function processExpression(dataNode, key, value){
var e = key.split(splitter);
if(e){
var e2 =[];
for (var i = 0; i < e.length; i++) {
if(e[i]!==''){e2.push(e[i]);}
}
e = e2;
if(e.length > 1){
var x = e[0];
var target = dataNode[x];
if(!target){
if(isNumber.test(e[1])){
dataNode[x] = [];
}
else{
dataNode[x] ={}
}
}
processExpression(dataNode[x], key.replace(first,''), value);
}
else if(e.length == 1){
dataNode[e[0]] = value;
}
else{
alert('This should not happen...');
}
}
}
}
Serialize javascript object to json and back
Checkout JSON.stringify()
and JSON.parse()
in JSON2 documentation
Example:
myData = JSON.parse(text); // from json string to js object
var myJSONText = JSON.stringify(myObject, replacer); // js object to json string
Related Topics
Difference Between == and === in JavaScript
Window.Close and Self.Close Do Not Close the Window in Chrome
How to Access React Instance (This) Inside Event Handler
What Does the Construct X = X || Y Mean
How to Do Case Insensitive String Comparison
Pass Variables by Reference in JavaScript
Difference Between Null and Undefined in JavaScript
Get the Name of an Object'S Type
How to Get the Full Object in Node.Js'S Console.Log(), Rather Than '[Object]'
Why Does a Regexp With Global Flag Give Wrong Results
Uncaught Referenceerror: $ Is Not Defined
Check Variable Equality Against a List of Values
Merge/Flatten an Array of Arrays
How to Access the Contents of an Iframe With JavaScript/Jquery