Storing compressed json data in local storage

此生再无相见时 提交于 2019-12-31 10:34:12

问题


I want to store JSON data in local storage. Sometimes data stored could be more than 5MB(max threshold allowed by browsers for each domain). Is there anyway i can compress or zip the data and store it in local storage ? How much latency it will add if compression and decompression for every JS function on large data ?

I am using this json data to display on webpage..... It is like a static data which is mostly same for entire session. we provide actions like search, filter etc on this json data...Whenever user enters a keyword, i search in this json data which is stored in local storage instead of making call to server


回答1:


As localStorage functionality seems to be limited to handle only string key/value pairs what you can do is to use lz-string library to stringify the json object and compress it before storing it in localStorage

Usage

// sample json object
var jsonobj = {'sample': 'This is supposed to be ling string', 'score': 'another long string which is going to be compressed'}

// compress string before storing in localStorage    
localStorage.setItem('mystring', LZString.compress(JSON.stringify(jsonobj)));

// decompress localStorage item stored
var string = LZString.decompress(localStorage.getItem('mystring'))

// parse it to JSON object
JSON.parse(string);

Check jsfiddle http://jsfiddle.net/raunakkathuria/7PQtC/1/ as i have copied the complete script so look for running code at end of fiddle



来源:https://stackoverflow.com/questions/20773945/storing-compressed-json-data-in-local-storage

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