SDH
SDH

Reputation: 391

JSON object returns as undefined

I am confused as to why this is alterting as "undefined". Any help would be much appreciated as I am stuck on an important project.

JavaScript

$.getJSON("item-data.json", function(results) {
        $.each(results, function(index) {
            alert(results[index].CatalogEntryView);
        });
    });

JSON Data is a BIG file. It starts out with the first object defined as "CatalogEntryView" with a long list of nested properties.

 {
   "CatalogEntryView": [
     {
      "CustomerReview": [

Using the following code recommended in one of the answers below returns the following to the console:

Recommended Code

 $.each(results.CatalogEntryView, function(index, item) {
        console.dir(item.CustomerReview); 
 });

Console Output in Chrome

Upvotes: 0

Views: 325

Answers (1)

curveball
curveball

Reputation: 4505

If I understood you correctly, try this:

 $.each(results.CatalogEntryView, function(index, item) {
        console.dir(item.CustomerReview); 
 });

It should fit the structure of your json file. Another question is what you really want to get...

results from your code should be iterable:

jQuery.each( array, callback ) - a generic iterator function, which can be used to seamlessly iterate over both objects and arrays. Arrays and array-like objects with a length property (such as a function's arguments object) are iterated by numeric index, from 0 to length-1. Other objects are iterated via their named properties.

Upvotes: 1

Related Questions