-3

How can access the following JSON object in JavaScript:

  {
    "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": {}
        }
    }
Glorious Kale
  • 1,273
  • 15
  • 25

1 Answers1

0

Via braces notation []. If you have only string as JSON, than use 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']);
Justinas
  • 41,402
  • 5
  • 66
  • 96
  • how can access this card name in javascript using for (var i in data) – MobileDeveloppement Dec 10 '14 at 08:41
  • @MobileDeveloppement use foreach or `$.each()` in jQuery. https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach http://api.jquery.com/jquery.each/ – Justinas Dec 10 '14 at 08:54