您好我试图使用PHPunserialize模块在javascript中反序列化此数组:
a:7:{s:13:"varPertinence";a:4:{i:0;s:5:"REGLT";i:1;s:2:"13";i:2;s:2:"15";i:3;s:2:"16";}s:10:"varSegment";N;s:12:"varSSegment1";N;s:12:"varSSegment2";N;s:7:"varVoie";N;s:4:"ingr";s:7:"cafaccentxA9ine";s:2:"mc";N;}
我收到了这个错误:
Unknown key type 'x' at position 192
在Php中,下面的表达可能会解决这个问题:
$myvar = preg_replace('!s:(\d+):"(.*?)";!e', "'s:'.strlen('$2').':\"$2\";'", $myvar );
这是我在js中试过的:
myvar = myvar.replace(!s:(\d+):"(.*?)";!e, "'s:'.$2.length.':\"$2\";'");
此转换是否正确?
你对这个错误有所了解吗?
这是一个工作示例(成功反序列化)
a:7:{s:13:"varPertinence";N;s:10:"varSegment";a:3:{i:0;s:2:"21";i:1;s:1:"7";i:2;s:2:"12";}s:12:"varSSegment1";N;s:12:"varSSegment2";N;s:7:"varVoie";N;s:4:"ingr";s:6:"citrus";s:2:"mc";N;}
谢谢
编辑:这是错误来自的两个函数(unserialise模块):
readType = function () {
var type = phpstr.charAt(idx);
idx += 2;
return type;
} //end readType
, readKey = function () {
var type = readType();
switch (type) {
case 'i': return readInt();
case 's': return readString();
default:
throw {
name: "Parse Error",
message: "Unknown key type '" + type + "' at position " +
(idx - 2)
};
} //end switch
}
答案 0 :(得分:0)
反序列化的问题来自特殊字符:éà...
我犯的错误是以正确的格式替换了这些字符:é : \u00e9
但我忘了在反序列化之前更新字符串的长度(添加长度('\ u00e9') - 1(替换字符为-1)
感谢大家的时间和帮助