简体   繁体   English

尝试将JSON对象解析为HTML表

[英]Trying to parse JSON objects to HTML table

I am using a jquery method to load JSON objects from the database. 我正在使用jquery方法从数据库加载JSON对象。 Then, I would want to display the record row by row in a HTML table. 然后,我想在HTML表中逐行显示记录。 As can be seen in my JS file, I have tried to use a for and for each loop to loop through the data records but none of them are working apparently. 从我的JS文件中可以看出,我尝试使用for和for每个循环遍历数据记录,但是它们显然都没有起作用。 My table seems to not display anything on the table or even error on result/ console log. 我的表似乎未在表上显示任何内容,甚至未在结果/控制台日志上显示错误。 However, there are data in my network log. 但是,我的网络日志中有数据。

The json record in the network log is: 网络日志中的json记录为:

data: [
      0:{
        "id": "1",
        "name": "John",
        "type": "Client"
      },
      1:{
        "id": "2",
        "name": "Damen",
        "type": "Agent"
      },
      2:{
        "id": "3",
        "name": "Aiden",
        "type": "Admin"
      }
    ]

The HTML page is: HTML页面是:

<table id="report" class="table table-bordered table-striped table-hover js-basic-example dataTable">
        <thead>
            <tr>
                <th>Id</th>
                <th>Name</th>
                <th>Type</th>           
            </tr>
        </thead>
        <tbody>         
        </tbody>
</table>
<script>
    app.viewReport();      
</script>

My JS page is: 我的JS页面是:

viewReport : function() {
               $.ajax({
                url: "/FIS/back-api/client/transaction/view",
                dataType: "json",
                method : 'GET',
                success : function(data){
                 for (var i = 0; i < data.length; i++) {
                    tr = $('<tr/>');
                    tr.append("<td>" + data[i].id+ "</td>");
                    tr.append("<td>" + data[i].name+ "</td>");
                    tr.append("<td>" + data[i].type+ "</td>");  
                    $('#report tbody').append(tr);
                }               
                }           
                /*$.each(data,function(i,item){
                            $('#report tr.odd').remove();
                            $("#report tbody").append(
                                "<tr>"
                                    +"<td>"+item.id+"</td>"
                                    +"<td>"+item.name+"</td>"
                                    +"<td>"+item.type+"</td>"                                                       
                                +"</tr>" )
                            })  
                }*/
    })
    }, 

John , your JSON is invalid , this is the closest valid JSON i prepared from sample you shared above : 约翰,您的JSON无效,这是我从上面共享的示例中准备的最接近的有效JSON:

[
  {
    "id": "1",
    "name": "John",
    "type": "Client"
  },
  {
    "id": "2",
    "name": "Damen",
    "type": "Agent"
  },
  {
    "id": "3",
    "name": "Aiden",
    "type": "Admin"
  }
]

Notice that for an array of objects 0 : {} format is invalid, so when you query it in a loop you're not receiving any result. 请注意,对于对象数组0 : {}格式无效,因此在循环中查询它时不会收到任何结果。

Incorrect : 不正确:

   0:{
        "id": "1",
        "name": "John",
        "type": "Client"
      }

Correct : 正确的:

    {
        "id": "1",
        "name": "John",
        "type": "Client"
      }

How to validate your JSON 如何验证您的JSON

Following sites will help you verify your json : 以下站点将帮助您验证json:

jsonlint jsonlint

jsoneditoronline jsoneditoronline

Answer 回答

Re-generate your JSON and then proceed as indicated in other answers. 重新生成您的JSON ,然后按照其他答案中的指示进行操作。

Also , i would advice against parsing the result as string ! 另外,我建议不要将结果解析为字符串! In my humble opinion, the proper way to do this is to return a valid json from Backend/server 以我的拙见,执行此操作的正确方法是从后端/服务器返回有效的json

try to append it like this 尝试像这样附加

 for (var i = 0; i < data.length; i++) {
    var htm = '';
    htm += '<tr>';
    htm += "<td>" + data[i].id.toString() + "</td>";
    htm += "<td>" + data[i].name.toString() + "</td>";
    htm += "<td>" + data[i].type.toString() + "</td>";  
    htm += '</tr>';
    $('#report tbody').append(htm);
 } 

Process your JSON data correctly like Winnie said in another answer of this question. 像Winnie在此问题的另一个答案中所说的那样,正确处理JSON数据。 Then you have not defined table row correctly. 则您没有正确定义表行。

for (i = 0; i < data.length; i++) {
    $('#report tbody').append(
        $('<tr>').append(
            $('<td>').text(data[i].id),
            $('<td>').text(data[i].name),
            $('<td>').text(data[i].type)
        )
    );
}

Your data array is having keys, which is incorrect json format, If its an array it should look like: 您的数据数组具有键,这是不正确的json格式,如果是数组,则其外观应为:

[
    {
        "id": "1",
        "name": "John",
        "type": "Client"
    },
    {
        "id": "2",
        "name": "Damen",
        "type": "Agent"
    },
    {
        "id": "3",
        "name": "Aiden",
        "type": "Admin"
    }
]

 var data = [{ "id": "1", "name": "John", "type": "Client" }, { "id": "2", "name": "Damen", "type": "Agent" }, { "id": "3", "name": "Aiden", "type": "Admin" } ]; for (var i = 0; i < data.length; i++) { var body = document.getElementById("tableBody"); var row = body.insertRow(); for (var item in data[i]) { var cell = row.insertCell(); cell.innerHTML = data[i][item]; } } 
 <html> <body> <table> <thead> <tr> <th>Id</th> <th>Name</th> <th>Type</th> </tr> </thead> <tbody id="tableBody"> </tbody> </table> </body> </html> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM