jquery - Creating JSON object with variable through JSON.stringify -
i'm having difficulty creating json object through json.stringify function. when create object manually, can reference correct object no problem, however, when create object through function (serializeobject), doesn't seem recognize it's object. both appear structured same way.
here's fiddle of i'm trying do: http://jsfiddle.net/cym37/1/
in fiddle, first i'm creating object , dumping div. i'm trying reference 1 piece of object , dump div (this part isn't working). can uncomment code see hardcoded json object working.
js
$('#createjson').click(function(){ var allitems = json.stringify($('#myform').serializeobject()); var manualjson = { "firstname":"john","lastname":"doe"}; $('#results').html(allitems); $('#results2').html(allitems.checkbox2); //$('#results2').html(manualjson.lastname); }); //serialize json function $.fn.serializeobject = function() { var o = {}; var = this.serializearray(); $.each(a, function() { if (o[this.name] !== undefined) { if (!o[this.name].push) { o[this.name] = [o[this.name]]; } o[this.name].push(this.value || ''); } else { o[this.name] = this.value || ''; } }); return o; };
any ideas why hardcoded json object working 1 create through function isn't? both appear structured identically.
it's because you're turning object string. example:
console.log(allitems[0]) // writes "{"
try this:
var allitems = $('#myform').serializeobject();
here's updated fiddle
Comments
Post a Comment