如何在本地浏览器中存储数据&从中取回

时间:2014-04-01 05:53:42

标签: javascript jquery json

我有以下json数据,我想将这些数据存储在我的浏览器中最后,如果用户从文本框中请求,我想从浏览器中获取这些数据。怎么做这个东西?

实际上,我是服务器端程序员,这是我的第二个javascript / jquery演示示例。我基本上是在创建演示的帮助下学习这些东西。请帮我学习。

我通过调用远程网站(例如www.google.com/finance/....

获得了jason数据
{
    "list": {
        "meta": {
            "type": "resource-list",
            "start": 0,
            "count": 168
        },
        "resources": [{
                "resource": {
                    "classname": "Quote",
                    "fields": {
                        "name": "USD/KRW",
                        "price": "1062.280029",
                        "symbol": "KRW=X",
                        "ts": "1396294510",
                        "type": "currency",
                        "utctime": "2014-03-31T19:35:10+0000",
                        "volume": "0"
                    }
                }
            }, {
                "resource": {
                    "classname": "Quote",
                    "fields": {
                        "name": "SILVER 1 OZ 999 NY",
                        "price": "0.050674",
                        "symbol": "XAG=X",
                        "ts": "1396287757",
                        "type": "currency",
                        "utctime": "2014-03-31T17:42:37+0000",
                        "volume": "217"
                    }
                }
            }


        ]
    }
}

2 个答案:

答案 0 :(得分:2)

使用jQuery和Localstorage可以:

设置项目:

localStorage.setItem('myJSON',yourJSONString);

删除项目:

localStorage.removeItem('myJSON');

获取项目:

var JSONString = localStorage.getItem('myJSON');

答案 1 :(得分:0)

browser storage有几种类型,例如localStorage,它们都是内置的,可以直接使用。

  

存储对象是标准的最新成员。因此,它们可能并不存在于所有浏览器中..........使用cookie严重限制了可以保存的最大数据大小。

代码示例:

  function storeMyContact(id) {
    var fullname    = document.getElementById('fullname').innerHTML;
    var phone       = document.getElementById('phone').innerHTML;
    var email       = document.getElementById('email').innerHTML;
    localStorage.setItem('mcFull',fullname);
    localStorage.setItem('mcPhone',phone);
    localStorage.setItem('mcEmail',email);
  }

另一方面,localStorage可能还不够,因此,外部库可以实现利用存储中内置的浏览器并使数据库跨浏览器工作。

1- SQL,如DB sequelsphere (看起来非常适合举重!)

将直接从浏览器运行的查询的代码示例:

SELECT empl_id, name, age 
  FROM empl 
 WHERE age < 30 

2-JSON,如DB taffydb (看起来适合日常活动!)

// Create DB and fill it with records
var friends = TAFFY([
    {"id":1,"gender":"M","first":"John","last":"Smith","city":"Seattle, WA","status":"Active"},
    {"id":2,"gender":"F","first":"Kelly","last":"Ruth","city":"Dallas, TX","status":"Active"},
    {"id":3,"gender":"M","first":"Jeff","last":"Stevenson","city":"Washington, D.C.","status":"Active"},
    {"id":4,"gender":"F","first":"Jennifer","last":"Gill","city":"Seattle, WA","status":"Active"}   
]);

   // Find all the friends in Seattle
   friends({city:"Seattle, WA"});

3- jstorage是一个跨浏览器的键值存储数据库,用于在浏览器中本地存储数据 - jStorage支持所有主流浏览器,包括桌面(是 - 甚至是Internet Explorer 6)和移动设备。 / p>

如果您想要更多选项 - &gt;(client-side-browser-database