Storing JSON data in browser memory

走远了吗. 提交于 2019-12-19 16:52:19

问题


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.

Thanks


回答1:


To add to the solutions given, I'd also want to add a reference link Storing Objects in HTML5 localStorage where this question is discussed nicely.

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




回答2:


You can use HTML5 storage which gives you both local and session storage.

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;



回答3:


Use the HTML5 storage. This stores persistent data.

You can access it with localStorage["key"].



来源:https://stackoverflow.com/questions/18913079/storing-json-data-in-browser-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!