jQuery jquery将json数据对象保存在cookie中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4225030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:52:15  来源:igfitidea点击:

jquery save json data object in cookie

jquerycookiesjquery-cookie

提问by Marthin

How do I save JSON data in a cookie?

如何将 JSON 数据保存在 cookie 中?

My JSON data looks like this

我的 JSON 数据如下所示

$("#ArticlesHolder").data('15', {name:'testname', nr:'4',price:'400'});
$("#ArticlesHolder").data('25', {name:'name2', nr:'1', price:'100'});
$("#ArticlesHolder").data('37', {name:'name3', nr:'14', price:'60'});

And I want to do something like

我想做类似的事情

var dataStore = $.cookie("basket-data", $("#ArticlesHolder").data());

and to retrieve the data i want to load it into $("#ArticlesHolder")like

和检索数据我想将其加载到$("#ArticlesHolder")

$.each($.cookie("basket-data"), function(i,e){
 $("#ArticlesHolder").data(i, e);
});

does anyone know if I'm on the right track or should this be done in some other way? Simply put, how do i put and pull json data from a cookie?

有谁知道我是在正确的轨道上还是应该以其他方式完成?简单地说,我如何从 cookie 中放入和提取 json 数据?

回答by Nick Craver

You can serialize the data as JSON, like this:

您可以将数据序列化为 JSON,如下所示:

$.cookie("basket-data", JSON.stringify($("#ArticlesHolder").data()));

Then to get it from the cookie:

然后从cookie中获取它:

$("#ArticlesHolder").data(JSON.parse($.cookie("basket-data")));

This relies on JSON.stringify()and JSON.parse()to serialize/deserialize your data object, for older browsers (IE<8) include json2.jsto get the JSONfunctionality. This example uses the jQuery cookie plugin

这依赖于JSON.stringify()JSON.parse()序列化/反序列化您的数据对象,对于较旧的浏览器(IE<8),包括json2.js以获得该JSON功能。此示例使用jQuery cookie 插件

回答by Vitalii Korsakov

Now there is already no need to use JSON.stringifyexplicitly. Just execute this line of code

现在已经不需要JSON.stringify显式使用了。只需执行这行代码

$.cookie.json = true;

After that you can save any object in cookie, which will be automatically converted to JSON and back from JSON when reading cookie.

之后,您可以将任何对象保存在 cookie 中,当读取 cookie 时,cookie 会自动转换为 JSON 并从 JSON 返回。

var user = { name: "name", age: 25 }
$.cookie('user', user);
...

var currentUser = $.cookie('user');
alert('User name is ' + currentUser.name);

But JSON library does not come with jquery.cookie, so you have to download it by yourself and include into html page before jquery.cookie.js

但是 JSON 库没有自带 jquery.cookie,所以你必须自己下载它并在jquery.cookie.js之前包含到 html 页面中

回答by XMen

use JSON.stringify(userData)to coverty json object to string.

用于JSON.stringify(userData)将 json 对象转换为字符串。

var dataStore = $.cookie("basket-data", JSON.stringify($("#ArticlesHolder").data()));

and for getting back from cookie use JSON.parse()

并从使用 cookie 中恢复过来 JSON.parse()

var data=JSON.parse($.cookie("basket-data"))

回答by Eyal Ch

It is not good practice to save the value that is returned from JSON.stringify(userData)to a cookie; it can lead to a bug in some browsers.

将返回的值保存JSON.stringify(userData)到 cookie不是一个好习惯;它可能会导致某些浏览器出现错误。

Before using it, you should convert it to base64(using btoa), and when reading it, convert from base64(using atob).

在使用之前,您应该将其转换为base64(使用btoa),并且在读取它时,将其转换为base64(使用atob)。

val = JSON.stringify(userData)
val = btoa(val)

write_cookie(val)

回答by asimov

With serialize the data as JSON and Base64, dependency jquery.cookie.js :

将数据序列化为 JSON 和 Base64,依赖 jquery.cookie.js :

var putCookieObj = function(key, value) {
    $.cookie(key, btoa(JSON.stringify(value)));
}

var getCookieObj = function (key) {
    var cookie = $.cookie(key);
    if (typeof cookie === "undefined") return null;
    return JSON.parse(atob(cookie));
}

:)

:)

回答by uzername_not_found

Try this one: https://github.com/tantau-horia/jquery-SuperCookie

试试这个:https: //github.com/tantau-horia/jquery-SuperCookie

Quick Usage:

create - create cookie

check - check existance

verify - verify cookie value if JSON

check_index - verify if index exists in JSON

read_values - read cookie value as string

read_JSON - read cookie value as JSON object

read_value - read value of index stored in JSON object

replace_value - replace value from a specified index stored in JSON object

remove_value - remove value and index stored in JSON object

快速使用:

创建 - 创建 cookie

检查 - 检查存在

验证 - 如果 JSON 验证 cookie 值

check_index - 验证 JSON 中是否存在索引

read_values - 读取 cookie 值作为字符串

read_JSON - 读取 cookie 值作为 JSON 对象

read_value - 读取存储在 JSON 对象中的索引值

replace_value - 从存储在 JSON 对象中的指定索引中替换值

remove_value - 删除存储在 JSON 对象中的值和索引

Just use:

只需使用:

$.super_cookie().create("name_of_the_cookie",name_field_1:"value1",name_field_2:"value2"});
$.super_cookie().read_json("name_of_the_cookie");