-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.

4
  • 3
    Can you post the actual JSON being returned, not the visualisation of it in the console. It will make it much clearer to navigate. Commented Feb 6, 2015 at 11:53
  • Try this instead: data['d']['0']['itemData'] Commented Feb 6, 2015 at 11:54
  • Have you tried to parse it first? api.jquery.com/jquery.parsejson So json parse data and then console.log(it). Commented Feb 6, 2015 at 11:54
  • 2
    @DanielPanic jQuery will already do that for you. Also, if the console is showing it in that format it has already been converted to an object. Commented Feb 6, 2015 at 11:55

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.