JSON对象转QueryString /** * 对象转数据 [key=value] 格式 * @param {any} jsonObj */ function objToArray(jsonObj) { let fields = []; let keys = Object.keys(jsonObj); for (var i = 0; i keys.length; i++) { let key = keys[i]; let type =
/** * 对象转数据 [key=value] 格式 * @param {any} jsonObj */ function objToArray(jsonObj) { let fields = []; let keys = Object.keys(jsonObj); for (var i = 0; i < keys.length; i++) { let key = keys[i]; let type = typeof jsonObj[key]; if (type === 'object') { let subObjFields = objToArray(jsonObj[key]); if (subObjFields && subObjFields.length > 0) { let tempFields = subObjFields.map(function (x) { let parts = x.split('='); let tempKey = isNaN(key) ? key + '.' : '[' + key + '].'; if(parts[0].startsWith('[')) { tempKey = isNaN(key) ? key : '[' + key + ']'; } return tempKey + x; }); fields = fields.concat(tempFields); } } else { fields.push((isNaN(key) ? key : '[' + key + ']') + '=' + encodeURIComponent(jsonObj[key])); } } return fields; }