1

I have this type of html:

<input id='a1' name='a1' value='111' class='hey'/>
<input id='b1' name='b1' value='222' class='hey'/>
<input id='c1' name='b1' value='333' class='hey'/>

and I need to obtain this json from it:

{ a1: '111', b1: '222', c1: '333' }

anybody knows how ?

Omu
  • 69,856
  • 92
  • 277
  • 407
  • possible duplicate of [Serialize form to JSON with jQuery](http://stackoverflow.com/questions/1184624/serialize-form-to-json-with-jquery) – Daniel A. White Apr 12 '11 at 13:56

2 Answers2

4

That should be pretty easy:

var json = {};
$('input.hey').each(function() {
  json[this.name] = this.value;
});

There are serialization routines supplied by jQuery to (for example) vaccuum up a whole form, but in a case like this it's pretty easy to just roll your own.

Pointy
  • 405,095
  • 59
  • 585
  • 614
1
var json = {};

$('input.hey').each(function(index) {
  json[$(this).attr('name')] = $(this).val();
});
Krule
  • 6,468
  • 3
  • 34
  • 56