我一直在尝试解析以下Cookie内容。我正在使用Jquery cookie插件,但无法提取像total_items等密钥的值。我试图将字符串转换为对象但不能。
a:7:{s:10:"session_id";s:32:"40049f03b718307f940c56da070646e1";s:10:"ip_address";s:13:"xxx.xx.1x1.97";s:10:"user_agent";s:108:"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36";s:13:"last_activity";i:1489319410;s:9:"user_data";s:0:"";s:7:"facelog";s:8:"facebook";s:13:"cart_contents";a:5:{s:32:"34173cb38f07f89ddbebc2ac9128303f";a:6:{s:5:"rowid";s:32:"34173cb38f07f89ddbebc2ac9128303f";s:2:"id";s:2:"30";s:3:"qty";s:1:"1";s:5:"price";s:4:"3.50";s:4:"name";s:5:"Pulao";s:8:"subtotal";d:3.5;}s:32:"c16a5320fa475530d9583c34fd356ef5";a:6:{s:5:"rowid";s:32:"c16a5320fa475530d9583c34fd356ef5";s:2:"id";s:2:"31";s:3:"qty";s:1:"1";s:5:"price";s:4:"3.00";s:4:"name";s:10:"Plain Rice";s:8:"subtotal";d:3;}s:32:"6364d3f0f495b6ab9dcf8d3b5c6e0b01";a:6:{s:5:"rowid";s:32:"6364d3f0f495b6ab9dcf8d3b5c6e0b01";s:2:"id";s:2:"32";s:3:"qty";s:1:"1";s:5:"price";s:4:"2.00";s:4:"name";s:13:"Tandoori Roti";s:8:"subtotal";d:2;}s:11:"total_items";i:3;s:10:"cart_total";d:8.5;}}bd6efb422491bf1ae129c0492dc108b4
有人能让我走上正轨吗?
答案 0 :(得分:1)
这看起来是用PHP序列化的对象。我建议在创建cookie时将其转换为JSON而不是PHP序列化。如果你不能这样做(例如,因为你没有运行应用程序的服务器端),你可以使用PHP.js The description。
答案 1 :(得分:1)
这将使用普通的javascript:
将其转换为标准的javascript对象var cookie = 'a:7:{s:10:"session_id";s:32:"40049f03b718307f940c56da070646e1";s:10:"ip_address";s:13:"xxx.xx.1x1.97";s:10:"user_agent";s:108:"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36";s:13:"last_activity";i:1489319410;s:9:"user_data";s:0:"";s:7:"facelog";s:8:"facebook";s:13:"cart_contents";a:5:{s:32:"34173cb38f07f89ddbebc2ac9128303f";a:6:{s:5:"rowid";s:32:"34173cb38f07f89ddbebc2ac9128303f";s:2:"id";s:2:"30";s:3:"qty";s:1:"1";s:5:"price";s:4:"3.50";s:4:"name";s:5:"Pulao";s:8:"subtotal";d:3.5;}s:32:"c16a5320fa475530d9583c34fd356ef5";a:6:{s:5:"rowid";s:32:"c16a5320fa475530d9583c34fd356ef5";s:2:"id";s:2:"31";s:3:"qty";s:1:"1";s:5:"price";s:4:"3.00";s:4:"name";s:10:"Plain Rice";s:8:"subtotal";d:3;}s:32:"6364d3f0f495b6ab9dcf8d3b5c6e0b01";a:6:{s:5:"rowid";s:32:"6364d3f0f495b6ab9dcf8d3b5c6e0b01";s:2:"id";s:2:"32";s:3:"qty";s:1:"1";s:5:"price";s:4:"2.00";s:4:"name";s:13:"Tandoori Roti";s:8:"subtotal";d:2;}s:11:"total_items";i:3;s:10:"cart_total";d:8.5;}}bd6efb422491bf1ae129c0492dc108b4';
function parseRecursive(obj,str) {
var foundkey = false;
var key;
for(var i=0;i<str.length;) {
switch(str.charAt(i)) {
case "}":
i++;
return(i);
case "a":
i = str.indexOf("{",i) + 1;
if(foundkey == true) {
var obj2 = {}
i += parseRecursive(obj2,str.substring(i));
obj[key] = obj2;
foundkey = false;
}
else console.log("found { with no key");
break;
case "i":
i += 2;
j = str.indexOf(';',i);
obj[key] = parseInt(str.substring(i,j));
foundkey = false;
i = j+1;
break;
case "d":
i += 2;
j = str.indexOf(';',i);
obj[key] = parseFloat(str.substring(i,j));
foundkey = false;
i = j+1;
break;
case "s":
i = str.indexOf('"',i) + 1;
j = str.indexOf('"',i) + 1;
if(foundkey == false) {
key = str.substring(i,j-1);
foundkey = true;
}
else {
obj[key] = str.substring(i,j-1);
foundkey = false;
}
i = j+1;
break;
default:
i++;
break;
}
}
}
function parse(obj,str) {
// skip to the first open brace and start recursive object parsing there
var i = str.indexOf("{") + 1;
parseRecursive(obj,str.substring(i));
}
var obj = {};
parse(obj,cookie);
console.log(obj);