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 );
           });
3
  • raw00 is not JSON, is not a string and not decoded anywhere? Commented Nov 16, 2014 at 23:17
  • It is JSON - validated it at jsonlint.com - It is a return of a PHP function, which encodes it. Commented Nov 16, 2014 at 23:18
  • No, it's not. 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) Commented Nov 16, 2014 at 23:22

1 Answer 1

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"
            }
        ]
    }
};
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the answer and +1 for the suggestion - will look into that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.