Monday 30 January 2017

javascript - How to convert jQuery.serialize() data to JSON object?

Is there any better solution to convert a form data that is already serialized by jQuery function serialize(), when the form contains multiple input Array fields. I want to be able to convert the form data in to a JSON object to recreate some other informative tables. So tell me a better way to get the serialize string converted as a JSON object.





// Raf


// Bily
// bily@someemail.com


// Andy
// Andy@somwhere.com


// Adam
// Adam@herenthere.com



The jquery method applied to get the data



var MyForm = $("#sampleform").serialize();
/** result : MyName=Raf&friendname[]=Billy&fiendemail[]=bily@someemail.com&friendname[]=Andy&fiendemail[]=Andy@somwhere.com&friendname[]=Adam&fiendemail[]=Adam@herenthere.com

*/


how do I make this data in to a JSON object?
which should have the following example JSON data from the above form.



{
"MyName":"raf",
"friendname":[
{"0":"Bily"},

{"1":"Andy"},
{"2":"Adam"}
],
"friendemail":[
{"0":"bily@someemail.com"},
{"1":"Andy@somwhere.com"},
{"2":"Adam@herenthere.com"}
]
}

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...