猿问

使用base64解密后拿到json字符串 用json.parse()方法为什么不能转换成对象?

<script src="js/base64.js"></script>

<script>


var theRequest = new Object();

var url='?params=eyJhZHZhbmNlZF9wYXlfcHJpY2UiOjY2NiwiYXV0b19jb2xvciI6IuWNoeWFtumHkSIsImF1dG9fbW9kZWxfbmFtZSI6IjEuNUwg6Ieq5YqoIOe7j+WFuOWeiyIsImZpbmFsX3ByaWNlIjo1NTU1LCJpbnRlcmlvciI6IumAgOaNoui0pyIsInJlYWxfcHJpY2UiOjY2NjYsInJlZmVyX3ByaWNlIjoiMTAuMTnkuIciLCJzZWxsZXJfbW9iaWxlIjoiMTM5ODI2MTUzMDciLCJzZWxsZXJfbmFtZSI6IuiZq+WmueWmuSIsInN1Yl9icmFuZF9uYW1lIjoi5Yev6LaKIiwic3VwcGxpZXJfbmFtZSI6IuW8mOmbheWIq+WFiyIsInllYXIiOiIyMDEzIn0=';

var  base = new Base64();

if (url.indexOf("?") != -1) {


   var str = url.substr(1);

   var strs = str.split("&");

   for (var i = 0; i < strs.length; i++) {

       theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1]);

   }

}


var result = theRequest.params;    //取到=后的值

console.log(result)

var strjson = base.decode(result);//base64解密

console.log(strjson)

var data = JSON.parse(strjson);   //这里就报错   

console.log(data);

</script>

不能转成json对象 引入了 base64解密插件 求大神帮忙下

牛魔王的故事
浏览 759回答 1
1回答

慕标琳琳

你获取params参数时用了strs[i].split("=")的方式,导致最后base64的值的最后一位=缺失。补充:for (var i = 0; i < strs.length; i++) {&nbsp; &nbsp;var splitIndex = url.indexOf("=");&nbsp; &nbsp;theRequest[strs[i].substr(0, splitIndex - 1)] = strs[i].substr(splitIndex);}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答