Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function jsonToFormData($json) {
- function isObject($o) { return typeof $o == 'object' && $o !== null; }
- function visit($json, $arr, $prefix) {
- var t0, i, j;
- t0 = $arr || [];
- $prefix = $prefix || '';
- for (i in $json) {
- if ( typeof $json[i] == 'function' ) continue;
- if ( Array.isArray($json[i]) ) {
- for (j in $json[i]) {
- if ( typeof $json[i][j] == 'function' ) continue;
- if ( isObject($json[i][j]) )
- t0.concat(visit($json[i][j], t0, (($prefix ? $prefix + '.' : '') + i + '[' + j +']')));
- else
- t0.push(($prefix ? $prefix + '.' : '') + i +'[' + j + ']' + '=' + encodeURIComponent($json[i][j]));
- }
- $prefix = '';
- continue;
- }
- if ( isObject($json[i]) ) t0.concat(visit($json[i], t0, (($prefix ? $prefix + '.' : '') + j)));
- else t0.push(($prefix ? $prefix + '.' : '') + i + '=' + encodeURIComponent($json[i]));
- }
- return t0;
- }
- return visit($json).join('&');
- }
- console.log(jsonToFormData({data:[{a:1, b:2, c:3}, {a:2, b:3, c:4}], msg:'aa'}));
- // data[0].a=1&data[0].b=2&data[0].c=3&data[1].a=2&data[1].b=3&data[1].c=4&msg=aa
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement