【发布时间】:2018-04-06 21:20:48
【问题描述】:
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”。我只是想附加表 td 中的值和 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>"
);
});
任何帮助将不胜感激。
【问题讨论】:
-
你能给出你预期的输出吗?