views:

8111

answers:

4

Hello,

I am returning a List<> from a webservice as a List of JSON objects. I am trying to use a for loop to iterate through the list and grab the values out of the properties. This is a sample of the returning JSON:

{"d":[{"__type":"FluentWeb.DTO.EmployeeOrder",
 "EmployeeName":"Janet Leverling",
 "EmployeeTitle":"Sales Representative",
 "RequiredDate":"\/Date(839224800000)\/",
 "OrderedProducts":null}]}

So I am trying to extract the contents using something like this:

function PrintResults(result) {

for (var i = 0; i < result.length; i++) { 
    alert(result.employeename);
}

How should this be done?

+3  A: 

Be careful, d is the list.

for (var i = 0; i < result.d.length; i++) { 
    alert(result.d[i].EmployeeName);
}

By the way your sample output looks invalid.

Burcu Dogan
Thanks.. but that doesn't return anything either.
Nick
It should. That is correct. JavaScript is case sensitive. EmployeeName
Chad Grant
+1  A: 

It's close! Try this:

for (var prop in result) {
    if (result.hasOwnProperty(prop)) {
        alert(result[prop]);
    }
}

Update:

If your result is truly is an array of one object, then you might have to do this:

for (var prop in result[0]) {
    if (result[0].hasOwnProperty(prop)) {
        alert(result[0][prop]);
    }
}

Or if you want to loop through each result in the array if there are more, try:

for (var i = 0; i < results.length; i++) {
    for (var prop in result[i]) {
        if (result[i].hasOwnProperty(prop)) {
            alert(result[i][prop]);
        }
    }
}
Cory Larson
This gets me closer.. It still just alerts with [object Object],[object Object]....
Nick
+2  A: 

Since you are using jQuery, you might as well use the each method... Also, it seems like everything is a value of the property 'd' in this JS Object [Notation].

$.each(result.d,function(i) {
    // In case there are several values in the array 'd'
    $.each(this,function(j) {
        // Apparently doesn't work...
        alert(this.EmployeeName);
        // What about this?
        alert(result.d[i][j]['EmployeeName']);
        // Or this?
        alert(result.d[i][j].EmployeeName);
    });
});

That should work. if not, then maybe you can give us a longer example of the JSON.

Edit: If none of this stuff works then I'm starting to think there might be something wrong with the syntax of your JSON.

KyleFarris
I found the solution the hard way (not JQuery). I will try this out asap. I'd rather do it your way. I'll make sure to post the solution.
Nick
So this very, very close. Now i am looping through the array of objects. In FireBug I can see the values in the properties but the alert just returns "Undefined". How can I get the string, dates, etc out of the properties?
Nick
A: 

had same problem today, Your topic helped me so here goes solution ;)

 alert(result.d[0].EmployeeTitle);
Thank you. This is the conclusion I came to as well. Here is a great post that has the solution detailed. http://elegantcode.com/2009/05/04/jquery-ajax-with-class-arrays/Hope this helps others.
Nick