1

Here is my Json Object example:

{"UserName":Mike,"IsActive":0,"ChbxIsActive":false,"MyAccountsAvailable":[{"Id":"157A","MyAccount":"CHRIS MCEL","MyCheckBox":false,"Tags":null},{"Id":"157B","MyAccount":"DAN BONE","MyCheckBox":false,"Tags":null}

Heres my attempt for part of the object:

$.getJSON('/ManageUsers/GetCheckBoxesJson', { clientId: clientId, user: user }, function (data) {
            var items = [];
            $.each(data, function (key, val) {

                alert(key + " " + val);

            });

Of course I am successful getting the data from the properties in the object, except not sure how to iterate over the array. I get [Object, Object] If someone could show a sample that would be great.

1 Answers1

0

You will need a nested loop to iterate over the array:

$.each(data, function (key, val) {

    console.log(key + " " + val);

    if(key == 'MyAccountsAvailable') {
        $.each(val, function(subKey, subVal) {
            console.log(subKey + " " + subVal);
        });
    }

});

Or directly access the values:

console.log( data.MyAccountsAvailable[0].Id );
console.log( data.MyAccountsAvailable[1].Id );

Instead of testing for the key name, you could check if the value is an array. See this question for how to do that.

Community
  • 1
  • 1
MrCode
  • 63,975
  • 10
  • 90
  • 112