0

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 );
           });
Barmar
  • 741,623
  • 53
  • 500
  • 612
jwknz
  • 6,598
  • 16
  • 72
  • 115
  • `raw00` is not JSON, is not a string and not decoded anywhere? – Bergi Nov 16 '14 at 23:17
  • It is JSON - validated it at jsonlint.com - It is a return of a PHP function, which encodes it. – jwknz Nov 16 '14 at 23:18
  • [No, it's not](http://stackoverflow.com/q/2904131/1048572). JSON is always text. You've assigned a data structure to `raw00` (which is represented by the JSON-like object literal syntax in your program code) – Bergi Nov 16 '14 at 23:22

1 Answers1

1

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"
            }
        ]
    }
};
Barmar
  • 741,623
  • 53
  • 500
  • 612