Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Sunday, February 7, 2016

Loop through JSON object List

Loop through JSON object List


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?

Answer by Burcu Dogan for Loop through JSON object List


Be careful, d is the list.

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

Answer by C??? for Loop through JSON object List


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]);          }      }  }  

Answer by KyleFarris for Loop through JSON object List


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 by veN for Loop through JSON object List


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

 alert(result.d[0].EmployeeTitle);  

Answer by redcrowe for Loop through JSON object List


Here it is:

success:       function(data) {          $.each(data, function(i, item){              alert("Mine is " + i + "|" + item.title + "|" + item.key);          });      }  

Sample JSON text:

{"title": "camp crowhouse",   "key": "agtnZW90YWdkZXYyMXIKCxIEUG9zdBgUDA"}  

Answer by ramonhimera for Loop through JSON object List


I have the following call:

$('#select_box_id').change(function() {          var action = $('#my_form').attr('action');      $.get(action,{},function(response){          $.each(response.result,function(i) {                alert("key is: " + i + ", val is: " + response.result[i]);            });      }, 'json');      });  

The structure coming back from the server look like:

{"result":{"1":"waterskiing","2":"canoeing","18":"windsurfing"}}  

Answer by Denzil Sequeira for Loop through JSON object List


var d = $.parseJSON(result.d);  for(var i =0;i


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.