Yes, you will need to parse the name field yourself. There is no automated method of parsing a custom field. There are, of course, multiple methods of doing so.
NOTE: I'm assuming that your name="items[0][id]" field specifies that this must be the 0th item in the resulting array, and that such sets of <input> fields are not, necessarily, in ascending order by item # within the DOM. In other words, the item[N] should be controlling over it being the Qth <fieldset> in the <form>.
You could use serializeArray() and then process that data:
(function($){
var $form = $('form');
var data = $form.serializeArray();
var result = {items:[]};
data.forEach(function(input){
nameArray = input.name.split(/[[\]]/);
item = nameArray[1];
prop = nameArray[3];
if(typeof result.items[item] !== 'object'){
result.items[item]={};
}
if(typeof result.items[item][prop] !== 'undefined'){
//Consistency check the name attribute
console.log('Warning duplicate "name" property =' + input.name);
}
result.items[item][prop]=input.value;
});
console.log(result);
})(jQuery);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form>
<fieldset>
<h2>Product 1</h2>
<input type="hidden" name="items[0][id]" value="7">
<input type="text" name="items[0][name]" value="Book">
<input type="number" name="items[0][price]" value="5.7">
</fieldset>
<fieldset>
<h2>Product 2</h2>
<input type="hidden" name="items[1][id]" value="5">
<input type="text" name="items[1][name]" value="Pencil">
<input type="number" name="items[1][price]" value="2.5">
</fieldset>
</form>
Or, you could directly process it from the DOM:
(function($){
var result = {items:[]};
$('form fieldset input').each(function(){
nameArray = this.name.split(/[[\]]/);
item = nameArray[1];
prop = nameArray[3];
if(typeof result.items[item] !== 'object'){
result.items[item]={};
}
if(typeof result.items[item][prop] !== 'undefined'){
//Consistency check the name attribute
console.log('Warning duplicate "name" property =' + this.name);
}
result.items[item][prop]=this.value;
});
console.log(result);
})(jQuery);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form>
<fieldset>
<h2>Product 1</h2>
<input type="hidden" name="items[0][id]" value="7">
<input type="text" name="items[0][name]" value="Book">
<input type="number" name="items[0][price]" value="5.7">
</fieldset>
<fieldset>
<h2>Product 2</h2>
<input type="hidden" name="items[1][id]" value="5">
<input type="text" name="items[1][name]" value="Pencil">
<input type="number" name="items[1][price]" value="2.5">
</fieldset>
</form>