2

I am working with converting form data to json data. Here is my code:

  $.fn.serializeObject = function()
{
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
                o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';

        }
    });
    return o;
};

$(function() {
    $('form').submit(function() {

        $('#result').append(JSON.stringify($('form').serializeObject()));

        return false;
    });
});

the above code gives the following json string:

  {"TypeName":["type1","type2"],"rate":["23","24"],"no:ofTickets":["23","24"],"allowedMembers": ["member1","member2"]}   

But my need is to get json like this:

   {[
    {"TypeName":"type1","rate":"23","no:ofTickets":"23","allowedMembers":"member1"},
    {"TypeName":"type2","rate":"34","no:ofTickets":"56","allowedMembers":"member2"}
]}

how can i solve my problem??

Thanks

2
  • this data you are getting from server side ? Commented Nov 9, 2013 at 11:38
  • @Gaurav No actually this data is a FORM data from the clientSide Commented Nov 9, 2013 at 11:42

3 Answers 3

1

serializeArray already does exactly that, you just need to convert the data into your required format:

$.fn.serializeObject = function()
{
  var o = {};
  var a = this.serializeArray();
   $.each(a, function()
   {
     if (o[this.name] !== undefined)
     {
        if (!o[this.name].push) 
        {
            o[this.name] = [o[this.name]];
        }
        o[this.name].push(this.value || '');
    }
    else 
    {
        o[this.name] = this.value || '';
    }
 });
 return o;
};
Sign up to request clarification or add additional context in comments.

Comments

0
$.fn.serializeObject = function()
{
    var elements = []; // Define an empty array
    var o = {};
    var a = this.serializeArray();

    $.each(a, function() {
       ...

       elements.push(o); // Store your objects into the array
    });

    return elements; // return the array
}

Working example http://fiddle.jshell.net/f8Zkr/

Comments

0

Change your searialzieObject function to :-

$.fn.serializeObject = function()
{
    var o = [];
    var a = this.serializeArray();
    $.each(a, function() {
        var new_dict = {};   
        if (new_dict[this.name] !== undefined) {
            if (!new_dict[this.name].push) {
                new_dict[this.name] = [new_dict[this.name]];
            }
                new_dict[this.name].push(this.value || '');
        } else {
            new_dict[this.name] = this.value || '';

        }
        o.push(new_dict);
    });
    return o;
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.