-4

How will I access this data in javascript?

[
    {
        "itemData": [
            {
                "Key": "218",
                "Value": "اسلامشهر"
            },
            {
                "Key": "219",
                "Value": " بهارستان"
            },
            {
                "Key": "220",
                "Value": " پاكدشت"
            },
            {
                "Key": "221",
                "Value": " پرديس"
            },
            {
                "Key": "222",
                "Value": " پيشوا"
            },
            {
                "Key": "223",
                "Value": " تهران"
            },
            {
                "Key": "224",
                "Value": " دماوند"
            },
            {
                "Key": "225",
                "Value": " رباط كريم"
            },
            {
                "Key": "226",
                "Value": " ري"
            },
            {
                "Key": "227",
                "Value": " شميرانات"
            },
            {
                "Key": "228",
                "Value": " شهريار"
            },
            {
                "Key": "229",
                "Value": " فيروز كوه"
            },
            {
                "Key": "230",
                "Value": " قدس"
            },
            {
                "Key": "231",
                "Value": " قرچك"
            },
            {
                "Key": "232",
                "Value": " ملارد"
            },
            {
                "Key": "233",
                "Value": " ورامين"
            }
        ]
    }
]

I used AJAX in JSON format. I tried access JSON like:

success: function (data) {
    $.each(data.d[0].itemData, function (index, element) {
        alert( data.d[0].itemData[index].key);
    })
}

but not getting output.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Mike
  • 159
  • 4
  • 14

1 Answers1

2

There is no d property in your JSON, so you can simply access data[0].itemData. Try this:

$.each(data[0].itemData, function (index, element) {
    console.log(element.Key);
})

Example fiddle

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339