The*_*ell 4 javascript json google-chrome google-chrome-extension
我想将json保存到chrome或本地存储.我还需要能够添加项目而不会丢失其他项目.
更新
var local = (function(){
var setData = function(key,obj){
var values = JSON.stringify(obj);
localStorage.setItem(key,values);
}
var getData = function(key){
if(localStorage.getItem(key) != null){
return JSON.parse(localStorage.getItem(key));
}else{
return false;
}
}
var updateDate = function(key,newData){
if(localStorage.getItem(key) != null){
var oldData = JSON.parse(localStorage.getItem(key));
for(keyObj in newData){
oldData[keyObj] = newData[keyObj];
}
var values = JSON.stringify(oldData);
localStorage.setItem(key,values);
}else{
return false;
}
}
return {set:setData,get:getData,update:updateDate}
})();
Run Code Online (Sandbox Code Playgroud)
你怎么用的?
如果要设置值:
var a = {'test':123};
local.set('valueA',a);
Run Code Online (Sandbox Code Playgroud)
当你想获得价值时:
var a = local.get('valueA')
Run Code Online (Sandbox Code Playgroud)
想要更新值或插入新值时
var b = {'test':333,'test2':555};
local.set('valueA',b);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3754 次 |
| 最近记录: |