如何在JavaScript中访问JSON对象

时间:2014-12-10 07:58:47

标签: javascript json

如何在JavaScript中访问以下JSON对象:

  {
    "data": {
        "1": {
        "id": "1",
        "name": "ZAIN",
        "cards": {
        "12": {
        "id": "12",
        "name": "Zain 5000",
        "count": "4",
        "value": "Z5",
        "price": "5000",
        "barcode": "654123987456",
        "position": "0",
        "days": "15",
        "howtouse": "*101# Zain Recharge Code # and OK",
        "background": null
        },
        "13": {},
        "14": {},
        "24": {},
        "25": {},
        "26": {}
        }
        },
        "2": {},
        "3": {},
        "4": {}
        }
    }

1 个答案:

答案 0 :(得分:0)

通过大括号表示法[]。如果您只有字符串作为JSON,那么使用JSON.parse(text)

var data = {
  "data": {
    "1": {
      "id": "1",
      "name": "ZAIN",
      "cards": {
        "12": {
          "id": "12",
          "name": "Zain 5000",
          "count": "4",
          "value": "Z5",
          "price": "5000",
          "barcode": "654123987456",
          "position": "0",
          "days": "15",
          "howtouse": "*101# Zain Recharge Code # and OK",
          "background": null
        },
        "13": {},
        "14": {},
        "24": {},
        "25": {},
        "26": {}
      }
    },
    "2": {},
    "3": {},
    "4": {}
  }
}

alert("Card name: " + data['data'][1]['cards']['12']['name']);