Thursday, 15 August 2013

jquery - Convert Javascript serialize to Javascript object -



jquery - Convert Javascript serialize to Javascript object -

i have textarea id of 'dataarrayfield' has code in it, example:

item1: { type: 'foo', fieldname: 'bar'} }, item2: { type: 'cat', fieldname: 'dog' }

i run var content = $('#dataarrayfield').serialize(); on textarea, has nem of codeformat, returned info like:

codeformat=item1%3a+ etc etc

i utilize $.deparam(content) javascript plugin turn serialized string object.

this works, however, key returned, codeformat name, value text in field above, this:

what can can access info in passed object!

you can either set valid json string in textarea such

{"item1":{"type":"foo","fieldname":"bar"},"item2":{"type":"cat","fieldname":"dog"}}

or can utilize regular expressions in this working demo clean before applying json.parse(). @jibsales has pointed out, not utilize eval():

$(function() { var val = $('#dataarrayfield').val().replace(/[\s']/g,'').replace(/(\w+)/g,'"$1"'); val = '{' + val.replace(/\}\},/g,'},') + '}'; console.log( json.parse(val) ); });

javascript jquery arrays string object

No comments:

Post a Comment