how to work with json feed stored in localStorage in phonegap app?

前端 未结 2 2000
独厮守ぢ
独厮守ぢ 2021-02-06 12:46

Here\'s what I am doing,

Get request to my web server, response is in json. Using jquery templates to render that callback data in my app. Pretty straightforward, works

相关标签:
2条回答
  • 2021-02-06 13:21

    You need to use JSON like so:

    localStorage.setItem('foo', JSON.stringify(data));
    

    And then parse it:

    JSON.parse(localStorage.getItem('foo'))
    
    0 讨论(0)
  • 2021-02-06 13:26
    App.local = (function () {
      var self = {};
    
      self.get = function (key) {
        var b = localStorage.getItem(key);
        return b ? JSON.parse(b) : null;
      }
    
      self.set = function (key, value) {
        var b = JSON.stringify(value);
        localStorage.setItem(key, b);
      }
    
      return self;
    })();
    

    Now you have a nice interface to local storage,

    var local = App.local;
    local.set('foo', 'bar');
    var bar = local.get('foo');
    console.log(bar);
    
    0 讨论(0)
提交回复
热议问题