1. #1
    Sencha User
    Join Date
    Oct 2009
    Posts
    5
    Vote Rating
    0
    bcd is on a distinguished road

      0  

    Default Ext.urlEncode with nested objects/arrays support

    Ext.urlEncode with nested objects/arrays support


    I've made a new urlEncode function which can handle nested objects

    Example:
    Code:
    {
    	a:{
    		b: [1,2,3,4],
    		c: 5
    	},
    	d:{
    		e: {
    			f:6
    		}
    	},
    	g:7,
    	h:[8,9,10]
    }
    will encode to: a[b][]=1&a[b][]=2&a[b][]=3&a[b][]=4&a[c]=5&d[e][f]=6&g=7&h[]=8&h[]=9&h[]=10
    The code:
    Code:
    Ext.urlEncode = function(o, pre, parentKey){
        var buf = [];
    	if(Ext.type(o)=='array'){
    		for(i=0;i<o.length;i++) {
    			val = o[i];
    			if(Ext.type(val)=='array' || Ext.type(val)=='object') {
    				buf = buf.concat(Ext.urlEncode(val, '', (Ext.isDefined(parentKey) ? parentKey : "")+'[]'));
    			} else {
    				buf[buf.length] = {key:(Ext.isDefined(parentKey) ? parentKey+'[]' : i.toString()), value:val};
    			}
    		}
    	}
    	if(Ext.type(o)=='object'){
    		for(key in o) {
    			val = o[key];
    			if(Ext.type(val)=='array' || Ext.type(val)=='object') {
    				buf = buf.concat(Ext.urlEncode(val, '', (Ext.isDefined(parentKey) ? parentKey+'['+key+']' : key)));
    			} else {
    				buf[buf.length] = {key:(Ext.isDefined(parentKey) ? parentKey+'['+key+']' : key), value:val};
    			}
    		}
    	}
    	if(Ext.isDefined(parentKey)) {
    		return buf;
    	} else {
    		return (Ext.isEmpty(pre)?"":pre+"&") + buf.map(function(x){return (x.key)+'='+encodeURIComponent(x.value.toString());}).join('&');
    	}
    };

  2. #2
    Sencha User
    Join Date
    Oct 2007
    Location
    Iran
    Posts
    165
    Vote Rating
    0
    vahid4134 is on a distinguished road

      0  

    Default


    I wrote this
    http://www.extjs.com/forum/showthread.php?t=85170
    but forum moderator move my Thread to help. I dont know why!!!
    No honor is like knowledge

  3. #3
    Ext User
    Join Date
    Dec 2009
    Posts
    2
    Vote Rating
    0
    davitz38 is on a distinguished road

      0  

    Default


    Hi guys,
    I use this url encoder online tool to make sure I'm encoding the right way
    David

  4. #4
    Sencha User
    Join Date
    Oct 2007
    Location
    Iran
    Posts
    165
    Vote Rating
    0
    vahid4134 is on a distinguished road

      0  

    Default


    This method change array or object to url
    No honor is like knowledge

  5. #5
    Sencha - Community Support Team mystix's Avatar
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    6,236
    Vote Rating
    5
    mystix will become famous soon enough

      0  

    Default


    Quote Originally Posted by vahid4134 View Post
    I wrote this
    http://www.extjs.com/forum/showthread.php?t=85170
    but forum moderator move my Thread to help. I dont know why!!!
    i moved it back from Help from Extensions.

    i thought it was a question. my bad.