使用HTML将JSON数据转换为动态表

时间:2016-01-14 16:59:05

标签: javascript jquery json dynamic html-table

我一直在寻找我的问题的解决方案四天了,我仍然无法解决它。

我想使用JQuery和JavaScript将JSON字符串数据(从URL)转换为动态表。

我必须使用jQuery.getJSON()函数加载JSON字符串。 我不知道如何将这两件事结合在一起。

我一直在寻找,我仍然不理解它。对不起,如果我听起来真的很愚蠢,但我无法从中汲取一些东西。有人可以帮助我吗?

这是我的代码:

<html>
<head>
<h1> Inventory List </h1>
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="http://wt.ops.few.vu.nl/api/--------"></script>
</head>

<body>
<script>
$(document).ready(function () {
    $.getJSON(http://wt.ops.few.vu.nl/api/-------,
    function (json) {
        var tr;
        for (var i = 0; i < json.length; i++) {
            tr = $('<tr/>');
            tr.append("<td>" + json[i].category + "</td>");
            tr.append("<td>" + json[i].name + "</td>");
            tr.append("<td>" + json[i].amount + "</td>");
            tr.append("<td>" + json[i].location + "</td>");
            tr.append("<td>" + json[i].date + "</td>");
            $('#table').append(tr);
        }
    });
});

</script>

<table id= "table">
  <tr>
    <th> Name of product</th>
    <th> Category</th>
    <th> Amount</th>
    <th> Location</th>
    <th> Date</th>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td> 
  </tr>
  <tr>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td> 
    <td> </td>
  </tr>
</table>
    </body>
    </html>

2 个答案:

答案 0 :(得分:2)

我建议像这样使用JSONP:https://jsfiddle.net/Twisty/0trde6es/5/

$(document).ready(function() {

  getData();

  function getData() {
    $.ajax({
      url: 'https://jsfiddle.net/echo/jsonp/',
      method: 'GET',
      dataType: "jsonp",
      error: function(xhr, status, error) {
        console.log(status, error);
      },
      success: function(json) {
        var tr;
        $.each(json, function(k, v) {
          tr = $("<tr></tr>");
          tr.append("<td>" + v.name + "</td>");
          tr.append("<td>" + v.category + "</td>");
          tr.append("<td>" + v.amount + "</td>");
          tr.append("<td>" + v.location + "</td>");
          tr.append("<td>" + v.date + "</td>");
          $("#invList").append(tr);
        });
      }
    });
  }
});

由于该网站可能不在同一个域上,并且未使用HTTPS,因此我在a1中创建了结果数据以进行测试。我使用了以下测试数据:

[{"category": "Fruit", "name": "Banana", "amount": 15, "location": "Amsterdam", "date": "2014-10-05", "id": 13844}, {"category": "Fruit", "name": "Apple", "amount": 58, "location": "Amsterdam", "date": "2014-02-05", "id": 13845}, {"category": "Furniture", "name": "Chair", "amount": 3, "location": "Hilversum", "date": "2014-12-10", "id": 13846}, {"category": "Furniture", "name": "Table", "amount": 5, "location": "Rotterdam", "date": "2011-07-13", "id": 13847}]

使用$.each()只是处理对象数据的一种更快捷的方法。您的for()循环没有任何问题,可能会根据您的需要更好地运作。

答案 1 :(得分:0)

这应该有效

<html>
<head>
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
</head>

<body>
<h1> Inventory List </h1>
<script>
$(document).ready(function () {
    $.getJSON('http://wt.ops.few.vu.nl/api/--------',
    function (json) {
        var tr;
        for (var i = 0; i < json.length; i++) {
            tr = $('<tr/>');
            tr.append("<td>" + json[i].category + "</td>");
            tr.append("<td>" + json[i].name + "</td>");
            tr.append("<td>" + json[i].amount + "</td>");
            tr.append("<td>" + json[i].location + "</td>");
            tr.append("<td>" + json[i].date + "</td>");
            $('#table').append(tr);
        }
    });
});

</script>

<table id= "table">
  <tr>
    <th> Name of product</th>
    <th> Category</th>
    <th> Amount</th>
    <th> Location</th>
    <th> Date</th>
  </tr>

</table>
</body>
</html>
相关问题