tags:

views:

44

answers:

1

I have a WCF RESTful service that returns a complex object with the following format:

{"GetMatterSearchRESTResult":{"BreakDownBypracticeGroups":[],"BreakDownByCountry":[],"TotalMatterCount":0}}.

When I have this value return, I try to access it using the following code below:

if (executor.get_responseAvailable) { var serviceResults = executor.get_responseData();

                var prasevalues = eval('(' + serviceResults + ')');

                var mattersResults = prasevalues.GetMatterSearchRESTResult;
                for (var a = 0; a < mattersResults.length; a++) {
                    alert(mattersResults[a].TotalMatterCount);
                    var pgBreakDown = mattersResults[a].BreakDownBypracticeGroups;
                    for (var b = 0; b < pgBreakDown.length; b++) {
                        alert(pgBreakDown[b].DepartmentName + " " + pgBreakDown[b].Count);
                    }
                }
            }
            return false;

After the eval function called, i get an undefined value. Please help.

A: 

GetMatterSearchRESTResult isn't an array, it's an object, so matterResults won't have a length method. Make GetMatterSearchRESTResult an array of objects:

{"GetMatterSearchRESTResult":[{"BreakDownBypracticeGroups":[],"BreakDownByCountry":[],"TotalMatterCount":0}]}

Edit: Or, if that's not possible, you don't need to loop through the object

Bob
Or use `for var x in ...`
Jonathon
I thought of that, but he references each member by name so there's no real need to loop through. For instance, mattersResults[a].TotalMatterCount would just become mattersResults.TotalMatterCount
Bob