【问题标题】:Get data from Local Storage从本地存储中获取数据
【发布时间】:2023-03-20 17:16:01
【问题描述】:

我的本​​地存储中有以下数据:

[{
  "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"
      }]
    }
  }
}]

我需要访问ecommerce 的值。于是得到如下数据:

"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"
    }]
  }
}

我该怎么做?

【问题讨论】:

标签: javascript object local-storage


【解决方案1】:

首先你必须解析你拥有的 json

var data = JSON.parse(json);

然后您可以像这样访问任何数据键和值:

var ecom = data.ecommerce 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-07
    • 1970-01-01
    • 2023-02-06
    相关资源
    最近更新 更多