jquery - Url parameters to array/object -


with jquery.param can do:

var myobject = {   a: {     one: 1,     two: 2,     three: 3   },   b: [ 1, 2, 3 ] }; var recursiveencoded = $.param( myobject ); var recursivedecoded = decodeuricomponent( $.param( myobject ) );  recursivedecoded; // "a[]=foo&a[]=bar&b[]=1&b[]=2&b[]=3" 

but how got:

var myobject = {   a: {     one: 1,     two: 2,     three: 3   },   b: [ 1, 2, 3 ] }; 

from "a[one]=1&a[two]=2&a[three]=3&b[]=1&b[]=2&b[]=3" ?

try

var myobject = {   a: {     one: 1,     two: 2,     three: 3   },   b: [ 1, 2, 3 ] };  var json = json.stringify(myobject); var _arr = decodeuricomponent($.param(json.parse(json))); var arr = json.parse(json.stringify(_arr.split("=").join("&").split("&"))); var obj = []; var tempobj = []; var tempvals = [];     $.each(arr, function(index, value) {   if ( /\[/.test(value) ) {     var props = value.split(/\[|\]/).slice(0, 2);     tempobj.push(props);           };     if ( !/\[/.test(value) ) {     var vals = number(value);     tempvals.push(vals);   };       });     $.when($.each(tempobj, function(index, value) {    value.push(tempvals[index]) })) .done(function(data) {   $.each(data, function(index, value) {     obj.push(value.filter(boolean))   }); });     console.log(obj); 

edit (update)

utilizing forked version (without $.browser, returned uncaught typeerror: cannot read property 'msie' of undefined @ jsfiddle) of jquery bbq $.deparam() method , try

    var myobject = {       a: {         one: 1,         two: 2,         three: 3       },       b: [ 1, 2, 3 ]     };      var p = decodeuricomponent( $.param( myobject ));     var d = $.deparam( p );       console.log( p, d );     // a[one]=1&a[two]=2&a[three]=3&b[]=1&b[]=2&b[]=3     // object {a: object, b: array[3]} 

jsfiddle http://jsfiddle.net/guest271314/6p7tf/


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -