user3482559
user3482559

Reputation: 265

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? can anyone tel me steps to achieve this target?

i have jason data obtained by calling remote websites as below

{
    "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. I am a serverside programmer, this is my 3rd day of javascript programming. Hope someone can help me here

Upvotes: 2

Views: 2336

Answers (2)

Blu
Blu

Reputation: 4056

In your code 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

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

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

from above line you get your data back in JSON object form so you can now process this object.

Demo

Upvotes: 1

Halcyon
Halcyon

Reputation: 57709

JSON.stringify is a serialization function. The unserialize function is called JSON.parse.

Upvotes: 5

Related Questions