JSON编码的数组在保存为cookie时转换为字符串

发布于 2024-11-09 00:39:36 字数 1226 浏览 0 评论 0原文

我有一个简单的数组,我正在尝试对其进行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

墨洒年华 2024-11-16 00:39:36

改变这个:

JSON.stringify(ids);
setCookie(cookieName, ids, 1);

到这个:

var str = JSON.stringify(ids);
setCookie(cookieName, str, 1);

Change this:

JSON.stringify(ids);
setCookie(cookieName, ids, 1);

To this:

var str = JSON.stringify(ids);
setCookie(cookieName, str, 1);
才能让你更想念 2024-11-16 00:39:36

我没有看到您实际使用 JSON 字符串化的结果:

jQuery(document).ready(function(){
    var ids = ['1', '2'];
    setCookie(cookieName, JSON.stringify(ids), 1);
});

请尝试这样做

I don't see you actually using the result of the JSON stringification:

jQuery(document).ready(function(){
    var ids = ['1', '2'];
    setCookie(cookieName, JSON.stringify(ids), 1);
});

Try this instead

酒绊 2024-11-16 00:39:36

不要使用这些 cookie 功能,它们是有缺陷的。 setCookie 无法正确编码名称和值,并且 getCookie 对于具有通用后缀的 cookie 失败(请参阅 我对Javascript getCookie 函数的回答)。

对于 setCookie 我会使用这个:

function setCookie(name, value, days) {
    var expires = "";
    if (days > 0) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        expires = "; expires="+date.toGMTString();
    }
    document.cookie = encodeURIComponent(name)+"="+encodeURIComponent(value)+expires+"; path=/";
}

Don’t use these cookie functions, they are flawed. setCookie fails to encode the name and value properly and getCookie fails for cookies with a common suffix (see my answer to Javascript getCookie functions).

For setCookie I would use this:

function setCookie(name, value, days) {
    var expires = "";
    if (days > 0) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        expires = "; expires="+date.toGMTString();
    }
    document.cookie = encodeURIComponent(name)+"="+encodeURIComponent(value)+expires+"; path=/";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文