简体   繁体   中英

Convert form with form arrays into a vaild json object

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"}
               ]
   }

I found a library that handles this

form2js

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM