Posting form using javascript and json with nested objects

Posted by Tim on Stack Overflow See other posts from Stack Overflow or by Tim
Published on 2010-04-29T15:44:16Z Indexed on 2010/04/29 15:47 UTC
Read the original article Hit count: 196

Filed under:
|
|
|

I have an html form that has the following structure:

<input type="text" name="title" />
<input type="text" name="persons[0].name" />
<input type="text" name="persons[0].color" />
<input type="text" name="persons[1].name" />
<input type="text" name="persons[1].color" />

I would like to serialize this into the following json:

    {
            "title":"titlecontent",
            "persons":[
            {
                    "name":"person0name",
                    "color":"red"
            },
            {
                    "name":"person1name",
                    "color":"blue"
            }
            ]
    }

Notice that the number of persons can vary from case to case.

The structure of the html form can change, but the structure of the submitted json can't.

How is this done the easiest?

© Stack Overflow or respective owner

Related posts about JSON

Related posts about JavaScript