简体   繁体   中英

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); 
 });

Chrome中的控制台输出

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM