2

My form looks like this.

<form>
    <input type="text" name="pic" value="test" />
    <input type="text" name="person[0].name" value="Bob" />
    <input type="text" name="person[0].age" value="25" />
    <input type="text" name="person[1].name" value="Jim"/>
    <input type="text" name="person[1].age" value="30" />
</form>

Is their a method that can take in any form and if the name of several form elements is the same then make them into an array under the initial name in json.

The json object would ideally look like

    {
      "pic" : "test",
      "person":[
                {"name":"Bob", "age":"25"}, 
                {"name":"Jim", "age":"30"}
               ]
   }
JDurman
  • 21
  • 4
  • 1
    Thanks for pointing me to that I think I can modify that to serve my purpose. – JDurman Jul 30 '15 at 02:03
  • see http://stackoverflow.com/questions/1184624/convert-form-data-to-javascript-object-with-jquery/8407771#8407771 for assistance. one of the repos cited in the answer https://github.com/macek/jquery-serialize-object/blob/master/jquery.serialize-object.js is not quite going to quite work with the '.' (dot) syntax in your binding, but it comes pretty close. if you can use '[' bracket syntax the repo might just work. – monkeyhouse Jul 30 '15 at 02:03
  • Have you tried person[0][name] instead of person[0].name in your html form? – angelcool.net Jul 30 '15 at 03:25
  • The .name is how the framework binds it to the model – JDurman Jul 30 '15 at 14:41

1 Answers1

0

I found a library that handles this

form2js

JDurman
  • 21
  • 4