有点晚了,但我制作的这个解决方案可以处理递归和嵌套的数组/对象。
function is(className, object) {
return Object.prototype.toString.call(object) === '[object '+ className +']';
}
const DataEncoder = function() {
this.levels = [];
}
DataEncoder.prototype.__dataEncoding = function(data, actualKey = null) {
let finalString = '';
if (actualKey) {
this.levels.push(actualKey);
}
const keys = Object.keys(data);
const l = keys.length;
for(let a = 0; a < l; a++) {
const key = keys[a];
let value = data[key];
if (is('Object', value)) {
finalString += this.__dataEncoding(value, key);
} else if (is('Array', value)) {
const arrValSize = value.length;
for (let b = 0; b < arrValSize; b++) {
let arrVal = value[b];
if (actualKey) {
finalString += actualKey;
for(let c = 1; c < this.levels.length; c++) finalString += '[' + this.levels[c] + ']';
if (arrVal === undefined || arrVal === null) arrVal = '';
finalString += '[' + key + '][]=' + arrVal + '&';
} else {
if (arrVal === undefined || arrVal === null) arrVal = '';
finalString += key + '[]=' + arrVal + '&';
}
}
} else {
if (actualKey) {
finalString += this.levels[0];
for(let c = 1; c < this.levels.length; c++) finalString += '[' + this.levels[c] + ']';
if (value === undefined || value === null) value = '';
finalString += '[' + key + ']=' + value + '&';
} else {
if (value === undefined || value === null) value = '';
finalString += key + '=' + value + '&';
}
}
}
this.levels.pop();
return finalString;
}
DataEncoder.prototype.encode = function(data) {
if (!is('Object', data) || data === {}) return null;
return this.__dataEncoding(data).slice(0, -1);
}
使用方法:
const testData = {
name: "John",
age: 13,
skills: ['games', 'programming', 'reading', 'singing'],
invests: {
education: [120.3, 50.5],
kids: 70,
optical: {
north: 20.5,
south: 10.70,
west: 6,
east: [7]
},
deeper: {
first: {
landing: 5
}
}
}
};
const encoder = new DataEncoder();
encoder.encode(testData);
结果:
name=John&age=13&skills[]=games&skills[]=programming&skills[]=reading&skills[]=singing&invests[education][]=120.3&invests[education][]=50.5&invests[kids]=70&invests[optical][north]=20.5&invests[optical][south]=10.7&invests[optical][west]=6&optical[optical][east][]=7&invests[deeper][first][landing]=5
我知道需要使用encodeURIComponent方法,但它可以很容易地添加
编辑,改进
function is(className, object) {
return Object.prototype.toString.call(object) === '[object '+ className +']';
}
const DataEncoder = function() {
this.levels = [];
this.actualKey = null;
}
DataEncoder.prototype.__dataEncoding = function(data) {
let uriPart = '';
const levelsSize = this.levels.length;
if (levelsSize) {
uriPart = this.levels[0];
for(let c = 1; c < levelsSize; c++) {
uriPart += '[' + this.levels[c] + ']';
}
}
let finalString = '';
if (is('Object', data)) {
const keys = Object.keys(data);
const l = keys.length;
for(let a = 0; a < l; a++) {
const key = keys[a];
let value = data[key];
this.actualKey = key;
this.levels.push(this.actualKey);
finalString += this.__dataEncoding(value);
}
} else if (is('Array', data)) {
if (!this.actualKey) throw new Error("Directly passed array does not work")
const aSize = data.length;
for (let b = 0; b < aSize; b++) {
let aVal = data[b];
this.levels.push(b);
finalString += this.__dataEncoding(aVal);
}
} else {
finalString += uriPart + '=' + encodeURIComponent(data) + '&';
}
this.levels.pop();
return finalString;
}
DataEncoder.prototype.encode = function(data) {
if (!is('Object', data) || !Object.keys(data).length) return null;
return this.__dataEncoding(data).slice(0, -1);
}
现在它可以处理任何深度的嵌套数组/对象,这个编辑器的使用方式保持不变。
const testData = {
name: "John",
age: 13,
skills: ['games', 'programming', 'reading', 'singing'],
invests: {
education: [120.3, 50.5],
kids: 70,
optical: {
north: 20.5,
south: 10.70,
west: 6,
east: [7]
},
deeper: {
first: {
landing: 5,
arrayLike: [
{
despite: true,
superb: 'yes',
omg: {
kiss: ['la'],
maybe: {
thiss: {
wont: {
work: 'false'
}
}
}
},
incredible: ['lalolanda', 'raidcall', 'phase', [5], [{waw: '@wesome'}]],
}
]
}
}
}
};
const encoder = new DataEncoder();
encoder.encode(testData);
结果:
name=John&age=13&skills[0]=games&skills[1]=programming&skills[2]=reading&skills[3]=singing&invests[education][0]=120.3&invests[education][1]=50.5&invests[kids]=70&invests[optical][north]=20.5&invests[optical][south]=10.7&invests[optical][west]=6&invests[optical][east][0]=7&invests[deeper][first][landing]=5&invests[deeper][first][arrayLike][0][despite]=true&invests[deeper][first][arrayLike][0][superb]=yes&invests[deeper][first][arrayLike][0][omg][kiss][0]=la&invests[deeper][first][arrayLike][0][omg][maybe][thiss][wont][work]=false&invests[deeper][first][arrayLike][0][incredible][0]=lalolanda&invests[deeper][first][arrayLike][0][incredible][1]=raidcall&invests[deeper][first][arrayLike][0][incredible][2]=phase&invests[deeper][first][arrayLike][0][incredible][3][0]=5&invests[deeper][first][arrayLike][0][incredible][4][0][waw]=%40wesome