循环遍历JSON并附加到HTML表

时间:2018-04-06 12:34:39

标签: jquery html json

HTML

<table id="id_kbdata" cellspacing="0" cellpadding="0" >
</table>

JSON

[  
{  
  "id":"3",
  "title":"Doing Business In...",
  "businessSubjectAreas":[  
     {  
        "businessSubjectArea":"Market and Sell Products/Service"
     },
     {  
        "businessSubjectArea":"Deliver Products/Services"
     },
     {  
        "businessSubjectArea":"HR"
     },
     {  
        "businessSubjectArea":"Legal"
     },
     {  
        "businessSubjectArea":"Finance"
     },
     {  
        "businessSubjectArea":"Tax"
     },
     {  
        "businessSubjectArea":"Treasury"
     },
     {  
        "businessSubjectArea":"IT"
     }
  ],
  "attachmentFiles":[  
     {  
        "fileName":"test.pdf",
        "url":"http://google.com/test.pdf"
     }
  ],
  "error":null
 },
 {  
  "id":"65",
  "title":"Dialing Instructions",
  "businessSubjectAreas":[  
     {  
        "businessSubjectArea":"Administrative"
     }
  ],
  "attachmentFiles":[  

  ],
  "error":null
 },
 {  
  "id":"132",
  "title":"WA - Western Australia - Drilling Fluid Management",
  "businessSubjectAreas":[  
     {  
        "businessSubjectArea":"Market and Sell Products/Service"
     },
     {  
        "businessSubjectArea":"Deliver Products/Services"
     },
     {  
        "businessSubjectArea":"Legal"
     }
  ],
  "attachmentFiles":[  
     {  
        "fileName":"test.pdf",
        "url":"http://google.com/test.pdf"
     }
  ],
  "error":null
 },
 {  
  "id":"133",
  "title":"WA - Natural gas from shale and tight rock - Overview of WA regulatory framework",
  "businessSubjectAreas":[  
     {  
        "businessSubjectArea":"Market and Sell Products/Service"
     },
     {  
        "businessSubjectArea":"Deliver Products/Services"
     },
     {  
        "businessSubjectArea":"Legal"
     }
  ],
  "attachmentFiles":[  
     {  
        "fileName":"test.pdf",
        "url":"http://google.com/test.pdf"
     }
  ],
  "error":null
 }
]

这里我试图遍历上面的JSON响应并将结果值附加到HTML表中。但是无法做到,下面是我到目前为止尝试的内容。

实际上我很困惑如何使用JSON中的嵌套值,如“businessSubjectArea”。我只是想在表格中附加值,并在li的

中附加嵌套值
$.each(json, function(index, value) {
 $("#id_kbdata").append(
     " <tr><td style='text-align:left;font-family:arial;padding:5px 10px;border-bottom:1px solid #ccc;border-right:1px solid #ccc;width:33%;'>"  
     + this.title + 
     "</td><td style='text-align:left;font-family:arial;padding:5px 10px;border-bottom:1px solid #ccc;border-right:1px solid #ccc;width:33%;'> "

     + "<ul>" +
     $.each(this.businessSubjectAreas, function(index, value) {
        "<li>" + this.businessSubjectAreas.businessSubjectArea + "</li>"
     }); 
     + "</ul>" +         

     " </td></tr>"
 );
});

任何帮助都将受到高度赞赏。

2 个答案:

答案 0 :(得分:0)

包括&#34; tbody&#34;在表中,然后向其追加行

<table id="id_kbdata" cellspacing="0" cellpadding="0" >
    <tbody></tbody>
</table>

并附加为

var html = "";

html += "<tr>";
html += "   <td style='text-align:left;font-family:arial;padding:5px 10px;border-bottom:1px solid #ccc;border-right:1px solid #ccc;width:33%;'>";
html +=         this.title;
html += "   </td>";
html += "   <td style='text-align:left;font-family:arial;padding:5px 10px;border-bottom:1px solid #ccc;border-right:1px solid #ccc;width:33%;'>";
html += "       <ul>";

$.each(this.businessSubjectAreas, function(index, value) {
    html += "<li>" + value.businessSubjectArea + "</li>";
}); 

html += "       </ul>";
html += "   </td>";
html += "</tr>";

$("#id_kbdata > tbody").append(html);

希望有所帮助

答案 1 :(得分:0)

我想这会有所帮助。

注意:这是仅使用js完成的。

&#13;
&#13;
var jsonObj = [{"id":"3","title":"Doing Business In...","businessSubjectAreas":[{"businessSubjectArea":"Market and Sell Products/Service"},{"businessSubjectArea":"Deliver Products/Services"},{"businessSubjectArea":"HR"},{"businessSubjectArea":"Legal"},{"businessSubjectArea":"Finance"},{"businessSubjectArea":"Tax"},{"businessSubjectArea":"Treasury"},{"businessSubjectArea":"IT"}],"attachmentFiles":[{"fileName":"test.pdf","url":"http://google.com/test.pdf"}],"error":null},{"id":"65","title":"Dialing Instructions","businessSubjectAreas":[{"businessSubjectArea":"Administrative"}],"attachmentFiles":[],"error":null},{"id":"132","title":"WA - Western Australia - Drilling Fluid Management","businessSubjectAreas":[{"businessSubjectArea":"Market and Sell Products/Service"},{"businessSubjectArea":"Deliver Products/Services"},{"businessSubjectArea":"Legal"}],"attachmentFiles":[{"fileName":"test.pdf","url":"http://google.com/test.pdf"}],"error":null},{"id":"133","title":"WA - Natural gas from shale and tight rock - Overview of WA regulatory framework","businessSubjectAreas":[{"businessSubjectArea":"Market and Sell Products/Service"},{"businessSubjectArea":"Deliver Products/Services"},{"businessSubjectArea":"Legal"}],"attachmentFiles":[{"fileName":"test.pdf","url":"http://google.com/test.pdf"}],"error":null}];


// using createElement
var myTable = document.getElementById('id_kbdata');
for (var i = 0; i < jsonObj.length; i++) {
  var tbRow = document.createElement('tr');
  var tbData1 = document.createElement('td');
  tbData1.innerHTML = jsonObj[i]['title'];
  var tbData2 = document.createElement('td');
  var bsa = jsonObj[i]['businessSubjectAreas'];
  for (var j = 0; j < bsa.length; j++) {
    var li = document.createElement('li');
    li.innerHTML = bsa[j]['businessSubjectArea'];
    tbData2.append(li);
  }
  tbRow.append(tbData1);
  tbRow.append(tbData2);
  myTable.append(tbRow);
}

//using innerHTML
var html = "";
for (var i = 0; i < jsonObj.length; i++) {
  html += "<tr><td>" + jsonObj[i]['title'] + "</td><td>";
  for (var j = 0; j < bsa.length; j++) {
    html += "<li>" + bsa[j]['businessSubjectArea'] + "</li>";
  }
  html += "</td></tr>";
}
document.getElementById("id_kbdata_1").innerHTML = html;
&#13;
td {
  text-align: left;
  font-family: arial;
  padding: 5px 10px;
  border-bottom: 1px solid #ccc;
  border-right: 1px solid #ccc;
  width: 33%;
}
&#13;
<table id="id_kbdata" cellspacing="0" cellpadding="0"></table>
<br>
<table id="id_kbdata_1" cellspacing="0" cellpadding="0" style="color:red"></table>
&#13;
&#13;
&#13;