如何使用javascript在html表中打印json数据

时间:2018-08-22 18:40:03

标签: jquery arrays json

当我尝试用html打印数据时显示错误。

我想要打印出场的详细信息,例如名字,姓氏等。

请帮助, 预先感谢!

https://uhsaa.org/stats/leaders.json

$(document).ready(function(e) {
   var dmJSON = "https://uhsaa.org/stats/leaders.json?callback=?";
    $.getJSON( dmJSON, function(data) {
       $.each(data.Leaders.Athletes, function(i, f) {
          var tblRow = "<tr>" + "<td data-label='ID'>" + f.id + "</td>" + "<td  data-label='User Name'>" + f.user.username + "</td>" + "<td  data-label='URL'>"+ f.url + "</td>" +"</tr>"
           $(tblRow).appendTo("#entrydata tbody");
     });

    });
});

1 个答案:

答案 0 :(得分:1)

您可以尝试生成HTML,然后将其分配给表
希望这会有所帮助

$(document).ready(function(e) {
    var dmJSON = "https://cors.io/?https://uhsaa.org/stats/leaders.json";
    htmlBuilder = "";
    $.getJSON( dmJSON, function(data) {
       for(var i=0; i<data.Leaders.Athletes.length; i++){  
       htmlBuilder += "<tr>";
       htmlBuilder += "<td>" + data.Leaders.Athletes[i].Athlete.AthleteID + "</td>";
       htmlBuilder += "<td>" + data.Leaders.Athletes[i].Athlete.FirstName + "</td>";
       htmlBuilder += "<td>" + data.Leaders.Athletes[i].Athlete.Position + "</td>";
       htmlBuilder += "<td>" + data.Leaders.Athletes[i].Athlete.School + "</td>";
       htmlBuilder += "</tr>";
       }
       $("#entrydata").html(htmlBuilder);
     });
});
td, tr, table {
border: 1px solid black;
}
<script
  src="https://code.jquery.com/jquery-3.3.1.min.js"
  integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
  crossorigin="anonymous"></script>
  
<table style="border 1px" id="entrydata">
</table>