JSON编码的数组在保存为cookie时转换为字符串
我有一个简单的数组,我正在尝试对其进行 JSON 编码并将其设置为 cookie。我正在使用 json2.js 脚本编码为 JSON。我使用以下代码来设置 cookie:
jQuery(document).ready(function(){
var ids = ['1', '2'];
JSON.stringify(ids);
setCookie(cookieName, ids, 1);
});
function setCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
将数组转换为 JSON 并将其记录在控制台中后,我得到:
["1", "2"]
这就是我所期望的。然后,我使用以下函数读出 cookie
function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
当我读出 cookie 并将其记录在控制台中时,我得到:
1,2
提示它是一个字符串,不再是 JSON 编码的对象。我希望能够将 cookie 设置为 JSON 编码对象,将其读取并解析 JSON 对象,最后对数据执行操作。因此,我的问题是,如何将 JSON 编码的对象发送到 cookie,以便我在读出它时可以将其解析为 JSON?
一如既往的感谢!
I have a simple array that I'm trying to JSON encode and set as a cookie. I am using the json2.js script to encode to JSON. I am using the following code to set the cookie:
jQuery(document).ready(function(){
var ids = ['1', '2'];
JSON.stringify(ids);
setCookie(cookieName, ids, 1);
});
function setCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
After converting the array to JSON, and logging it in console, I get:
["1", "2"]
which is what I would expect. I then read out the cookie with the following function
function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
When I read out the cookie and log it in the console, I get:
1,2
suggesting that its a string and no longer a JSON encoded object. I would like to be able to set a cookie an a JSON encoded object, read it out and parse the JSON object, and finally perform operations on the data. My question is thus, how do you send a JSON encoded object to the cookie in such a way that I can parse it as JSON when I read it out?
Thanks as always!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
改变这个:
到这个:
Change this:
To this:
我没有看到您实际使用 JSON 字符串化的结果:
请尝试这样做
I don't see you actually using the result of the JSON stringification:
Try this instead
不要使用这些 cookie 功能,它们是有缺陷的。
setCookie
无法正确编码名称和值,并且getCookie
对于具有通用后缀的 cookie 失败(请参阅 我对Javascript getCookie 函数的回答)。对于
setCookie
我会使用这个:Don’t use these cookie functions, they are flawed.
setCookie
fails to encode the name and value properly andgetCookie
fails for cookies with a common suffix (see my answer to Javascript getCookie functions).For
setCookie
I would use this: