简体   繁体   English

如何从localStorage读取json格式的数据

[英]how to read json format data from localStorage

I created a fiddle here . 我在这里开了一个小提琴。

How to calculate the currencies values by reading data from the localStorage ? 如何通过从localStorage读取数据来计算货币值? can anyone tel me steps to achieve this target? 有人可以打电话给我实现这一目标的步骤吗?

i have jason data obtained by calling remote websites as below 我有通过以下远程网站获取的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"
                    }
                }
            }


        ]
    }
}

i am actually learning how to use javascript by dealing with localstorages. 我实际上正在学习如何通过与本地存储进行交互来使用javascript。 I am a serverside programmer, this is my 3rd day of javascript programming. 我是服务器端程序员,这是我进行JavaScript编程的第三天。 Hope someone can help me here 希望有人可以在这里帮助我

JSON.stringify is a serialization function. JSON.stringify是一个序列化函数。 The unserialize function is called JSON.parse . 反序列化函数称为JSON.parse

In your code localStorage.setItem('all_currencies',JSON.stringify(d)); 在您的代码中localStorage.setItem('all_currencies',JSON.stringify(d)); this line converts your JSON Object to String so it can store in localStorage Now for get data back from there you have to write 该行将您的JSON Object转换为String因此可以将其存储在localStorage Now中,以便从那里获取数据,您必须编写

var jsonStringFromLS = localStorage.getItem('all_currencies');

Now in jsonStringFromLS you have all data in string form to convert into Object pass this value into following function 现在在jsonStringFromLS您具有字符串形式的所有数据都可以转换为Object并将此值传递给以下函数

console.log(JSON.parse(jsonStringFromLS));

from above line you get your data back in JSON object form so you can now process this object. 从上面的行中,您可以将数据以JSON object形式返回,因此您现在可以处理此对象。

Demo 演示版

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM