0% found this document useful (0 votes)
119 views2 pages

Iterate JSON List in JavaScript

The document discusses different ways to iterate through a JSON object list returned from a web service and extract property values. It provides sample JSON data and several approaches to loop through the list and alert the "EmployeeName" value of each object, including using a basic for loop, jQuery each method, and for/in loop. The best way depends on the exact structure of the JSON response.

Uploaded by

I@Sydney
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
119 views2 pages

Iterate JSON List in JavaScript

The document discusses different ways to iterate through a JSON object list returned from a web service and extract property values. It provides sample JSON data and several approaches to loop through the list and alert the "EmployeeName" value of each object, including using a basic for loop, jQuery each method, and for/in loop. The best way depends on the exact structure of the JSON response.

Uploaded by

I@Sydney
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Loop through JSON object List

1 down vote favorite

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":"[Link]",
"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 < [Link]; i++) {


alert([Link]);
}

How should this be done?

Answer 1
for (var i = 0; i < [Link]; 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([Link]);
// 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

It's close! Try this:

for (var prop in result) {


if ([Link](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 < [Link]; i++) {


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

You might also like