Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to read data from json to View using jquery mvc

I have the following data in an array.

[{"FirstName":"Nancy","LastName":"Davolio","Title":"Sales Representative"},
{"FirstName":"Andrew","LastName":"Fuller","Title":"Vice President, Sales"}]

I want to present this data using jquery into a table like this :

 <table id="employee">
 <tr>
 <td>Nancy</td>
 <td>Davolio</td>
 <td>Sales Representative</td>
 ...
 </table>
like image 748
Francis Padron Avatar asked Dec 28 '25 07:12

Francis Padron


2 Answers

similar

$(document).ready(function() {
      var jsonp = '[{"Lang":"jQuery","ID":"1"},{"Lang":"C#","ID":"2"}]';
      var lang = '';
      var obj = $.parseJSON(jsonp);
      $.each(obj, function() {
          lang += this['Lang'] + "<br/>";
      });
      $('span').html(lang);
    });​
like image 97
Mr.LamYahoo Avatar answered Dec 30 '25 22:12

Mr.LamYahoo


I did something like this before:

var apiUrl = 'UrlOfData';
$(document).ready(function () {
    $.getJSON(apiUrl)
        .done(function (data) {
            for (var item in data){
                $('#people').append(item.FirstName);
            }
        })
        .fail(function (jqXHR, textStatus, err) {
            $('#people').text('Error: ' + err);
        });
});
like image 28
Daniel Dawes Avatar answered Dec 31 '25 00:12

Daniel Dawes