简体   繁体   中英

Accessing second array in a JSON decode using Jquery

I need to access the second array from a JSON decoded string, but I am having no luck.

The entire JSON string is displayed in var RAW00 , and then split into var RAW01 & var RAW02 . All 3 of these are for testing - RAW00 is identical to msg

When they are split - I can access either, depending on what variable I start of with, but when I use RAW00 I cannot access the tutor section.

I will provide more detail if required, but my question is:

How do I see and access the tutor array in the second $.each (nested) block?? ]

Thanks :-)

success: function(msg)
{
    var test = "";
    var raw00 = {
        "allData": [
            {
                "class2": [
                    {
                        "tid": "1",
                        "name": "Monday 2"
                    },
                    {
                        "tid": "1",
                        "name": "Monday Test"
                    }
                ]
            },
            {
                "tutor": [
                    {
                        "fname": "Jeffrey",
                        "lname": "Kranenburg"
                    },
                    {
                        "fname": "Jeffrey",
                        "lname": "Kranenburg"
                    }
                ]
            }
        ]
    };

    var raw01 = {
        "allData": [
            {
                "class2": [
                    {
                        "tid": "1",
                        "name": "Monday 2"
                    },
                    {
                        "tid": "1",
                        "name": "Monday Test"
                    }
                ]
            }
        ]
    };
    var raw02 = {
        "allData": [
            {
                "tutor": [
                    {
                        "fname": "Jeffrey",
                        "lname": "Kranenburg"
                    },
                    {
                        "fname": "Jeffrey",
                        "lname": "Kranenburg"
                    }
                ]
            }
        ]
    };

    $.each(raw00.allData, function(index, entry) 
           {  
               $.each(entry.class2, function (index, data) 
                      {
                          console.log(this.name);
                          test += '<tr><td>'+this.name+'</td>';
                      });

               $.each(entry.tutor, function (index, data) 
                      {
                          console.log(this.fname);
                          test += '<td>'+this.name+'</td></tr>';
                      });

               $('#all-courses-table-content').html( test );
           });

You need to check whether the current element of the array is an object with class2 property or tutor property.

$.each(raw00.allData, function(index, entry) {  
    if (entry.hasOwnProperty('class2')) {
        $.each(entry.class2, function (index, data) 
               {
                   console.log(this.name);
                   test += '<tr><td>'+this.name+'</td>';
               });
    }

    if (entry.hasOwnProperty('tutor')) {
        $.each(entry.tutor, function (index, data) 
               {
                   console.log(this.fname);
                   test += '<td>'+this.fname+'</td></tr>';
               });
    }

    $('#all-courses-table-content').html( test );
});

Things would probably be simpler if you redesigned the data structure. It generally doesn't make sense to have an array of objects when each object just has a single key and it's different for each. I suggest you replace the allData array with a single object, like this:

var raw00 = {
    "allData": {
        "class2": [
            {
                "tid": "1",
                "name": "Monday 2"
            },
            {
                "tid": "1",
                "name": "Monday Test"
            }
        ],
        "tutor": [
            {
                "fname": "Jeffrey",
                "lname": "Kranenburg"
            },
            {
                "fname": "Jeffrey",
                "lname": "Kranenburg"
            }
        ]
    }
};

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