-
-
Save HDDen/fa1ddc435c4d77ad11b2d3a840b298fa to your computer and use it in GitHub Desktop.
Convert JavaScript object to x-www-form-urlencoded format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (!window['hdden_JSON_to_URLEncoded']) { | |
window['hdden_JSON_to_URLEncoded'] = hdden_JSON_to_URLEncoded; | |
} | |
function hdden_JSON_to_URLEncoded(element, key, list) { | |
var list = list || []; | |
if (typeof (element) == 'object') { | |
for (var idx in element) | |
hdden_JSON_to_URLEncoded(element[idx], key ? key + '[' + idx + ']' : idx, list); | |
} else { | |
list.push(key + '=' + encodeURIComponent(element)); | |
} | |
return list.join('&'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var data = { | |
'users' : [ | |
{ | |
"id": 100, | |
"name": "Stefano" | |
}, | |
{ | |
"id": 200, | |
"name": "Lucia" | |
}, | |
{ | |
"id": 300, | |
"name": "Franco" | |
}, | |
], | |
'time' : +new Date | |
}; | |
console.log( | |
JSON_to_URLEncoded(data) | |
); | |
/* | |
Output: | |
users[0][id]=100&users[0][name]=Stefano&users[1][id]=200&users[1][name]=Lucia&users[2][id]=300&users[2][name]=Franco&time=1405014230183 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment