Dips
Dips

Reputation: 3290

Ajax retrieve json data

I am trying to retrieve json data using jquery ajax.

alert(data.EntryList.Entry.FirstName) //This returns undefined

I am trying to get the value of first name, last name etc.

Here is the code looks like

$.ajax({
        url: "",
        context: document.body,
        type: "GET",
        dataType: "jsonp",
        success: function(data) {


            console.log(data);

            alert(data.EntryList.Entry.FirstName)

        }


}); //Ajax End​

Console log screenshot below

enter image description here

Upvotes: 0

Views: 3837

Answers (3)

tarashish
tarashish

Reputation: 1965

In addition to what others have said you could also use $.map() or $.each() functions that JQuery provides to iterate over arrays.

var entries = data.EntryList.Entry;
$.each(entries, function(index,entry) {
  console.log(entry.FirstName);
});

or

var entries = data.EntryList.Entry;
$.map(entries, function(entry,index) {
  console.log(entry.FirstName);
});

And also data.EntryList[i].Entry.FirstName is an object . So alert may not be doing what you intend it to do. You should alert data.EntryList[i].Entry.FirstName.value

Upvotes: 2

xdazz
xdazz

Reputation: 160843

data.EntryList.Entry is an array.

var entries = data.EntryList.Entry;
for (var i = 0, l = entries.length; i < l; i++) {
  console.log(entries[i].FirstName.value);
  console.log(entries[i].LastName.value);
}

Upvotes: 3

Pete
Pete

Reputation: 635

try alert(data.EntryList.Entry[0].FirstName) as EntryList.Entry is an array

Upvotes: 6

Related Questions