Loop Through JSON Object List
Loop Through JSON Object List
Hello,
{"d":[{"__type":"FluentWeb.DTO.EmployeeOrder",
"EmployeeName":"Janet Leverling",
"EmployeeTitle":"Sales Representative",
"RequiredDate":"\/Date(839224800000)\/",
"OrderedProducts":null}]}
function PrintResults(result) {
Answer 1
for (var i = 0; i < result.d.length; i++) {
alert(result.d[i].EmployeeName);
}
Answer 2
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.
Answer 3
Update:
If your result is truly is an array of one object, then you might have to do this:
Or if you want to loop through each result in the array if there are more, try: