javascript 将 JSON 数据存储在浏览器内存中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18913079/
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
Storing JSON data in browser memory
提问by SharpCoder
I want to persist some JSON information in browser. Depending on user interaction with the application, I want to store 5-6 different JSON object into memory. What options I have to achieve this? Please suggest any library or plugin using which I can persist information in the browser.
我想在浏览器中保留一些 JSON 信息。根据用户与应用程序的交互,我想将 5-6 个不同的 JSON 对象存储到内存中。我有什么选择来实现这一目标?请建议我可以在浏览器中保留信息的任何库或插件。
Thanks
谢谢
回答by Rupam Datta
To add to the solutions given, I'd also want to add a reference link Storing Objects in HTML5 localStoragewhere this question is discussed nicely.
要添加到给出的解决方案中,我还想添加一个参考链接在 HTML5 localStorage中存储对象,其中很好地讨论了这个问题。
Below is the code
下面是代码
var testObject = { 'one': 1, 'two': 2, 'three': 3 };
// Put the object into storage
localStorage.setItem('testObject', JSON.stringify(testObject));
// Retrieve the object from storage
var retrievedObject = localStorage.getItem('testObject');
console.log('retrievedObject: ', JSON.parse(retrievedObject));
Courtesy: CMS
礼貌:CMS
回答by BenM
You can use HTML5 storage which gives you both local and session storage.
您可以使用 HTML5 存储,它为您提供本地和会话存储。
Local storage persists it in a local cache and can therefore be accessed again in the future, despite the browser being closed.
本地存储将其保存在本地缓存中,因此即使浏览器已关闭,也可以在将来再次访问。
Session storage will only store the information for that particular session and will be wiped once the session ends.
会话存储将只存储该特定会话的信息,并在会话结束后被擦除。
e.g.
例如
//get item from storage
var foo = localStorage["bar"];
//set item in storage.
localStorage["bar"] = foo;
回答by Shylux
Use the HTML5 storage. This stores persistent data.
使用 HTML5 存储。这存储持久数据。
You can access it with localStorage["key"]
.
您可以使用localStorage["key"]
.