如何为javascript对象数组创建cookie

时间:2015-02-18 20:21:06

标签: arrays javascript-events javascript

如何在JavaScript中从cookie创建和读取对象数组的值? [{name =“sss”,age =“33”} ,{name =“ll”,age =“22”}]

1 个答案:

答案 0 :(得分:1)

你必须" stringify"数组并将字符串存储在cookie中,如:

var myArray = [{name="sss", age="33"} , {name="ll", age="22"}];
var cookieStr = JSON.stringify(myArray);
// Do whatever you want with your cookie

然后你将字符串稍后退出(作为字符串)并解析它:

var cookieStr = ...; // Get it out of the cookie
var myArray = JSON.parse(cookieStr);