我有一个非常特殊的问题需要解决:
我有以下序列化查询字符串:
a=a2&b.c=c2&b.d.e=e2&b.d.f=f2
被反序列化成以下对象:
{
a: "a2",
b.c: "c2",
b.d.e: "e2",
b.d.f: "f2"
}
使用以下解析器(在处理扁平对象时非常好用!)
function parse(string){
string =
'{"' + //root
string
.replace(/&/g, '","') //replace '&' with ','
.replace(/=/g,'":"')+ //replace '=' with ':'\
'"}'; //close root
return JSON.parse(string,function(key, value){ //handle URI issues
var ret;
if(key===""){ //null key means that we have something wrong with the encoding, probably escaped shit
ret = value;
}
else{
ret = decodeURIComponent(value); //decode escaped stuff
}
return ret;
});
}
需要将此解析为一个多维对象,该对象代表键中的 .
符号,如下所示:
{
a:"a2",
b:{
c: "c2",
d:{
e:"e2",
f:"f2"
}
}
}
任何帮助都将是惊人的。我已经尝试了在过去几个小时内重复这个过程,但我的大脑已经散架了,解决方案中没有任何乐趣。
如果有另一种方法将N维JavaScript对象解析为URI,然后再解析回JavaScript对象(两个函数),那么我很愿意听听建议。