简体   繁体   中英

Get data from Local Storage

I've the following data in my local storage:

[{
  "event": "addToCart",
  "eventLabel": "Test Product",
  "ecommerce": {
    "currencyCode": "USD",
    "add": {
      "products": [{
        "name": "Test Product",
        "id": "78789",
        "price": "429.00",
        "category": "Catalog Page",
        "list": "Massage\/Massage Chairs\/Portable Massage Chairs",
        "quantity": "1"
      }]
    }
  }
}]

and I need to access to the value of ecommerce . So obtaining the following data:

"ecommerce": {
  "currencyCode": "USD",
  "add": {
    "products": [{
      "name": "Test Product",
      "id": "78789",
      "price": "429.00",
      "category": "Catalog Page",
      "list": "Massage\/Massage Chairs\/Portable Massage Chairs",
      "quantity": "1"
    }]
  }
}

How can I do it?

First you have to parse the json you have

var data = JSON.parse(json);

then you can access any data key and values like this:

var ecom = data.ecommerce 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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