以下是JSON结构:
{
"action": "organizationQueryResponse",
"status": "SUCCESS",
"matchCount": "2",
"organizationDetailsList": [
{
"organizationDetails": {
"organizationID": "xxxxx",
"organizationName": "xxxx",
"parentOpCoName": "yyyy",
"registeredEmailID": "zzzz",
"registeredPhoneNo": "xxxx"
}
},
{
"organizationDetails": {
"organizationID": "xxxxx",
"organizationName": "xxxx",
"parentOpCoName": "yyyy",
"registeredEmailID": "zzzz",
"registeredPhoneNo": "xxxx"
}
}
]
}
我需要的是与 organizationDetails 键对应的值,并将这些值附加到<tr>
中。我尝试过,但总是得到一个凌乱的代码。请帮忙写一个jQuery函数。
这就是我的尝试。
success : function(response) {
//process response here
alert('Success !!!');
$.each(response, function(key,val){
if(key == 'organizationDetailsList'){
$.each(val, function(keys,vals){
$.each(vals, function(keys,values){
alert("key : "+keys+" ; value : "+values);
});
});
}
});
}
答案 0 :(得分:1)
您可以使用解决方案https://jsfiddle.net/jLr5vapn/
var data = {
"action": "organizationQueryResponse",
"status": "SUCCESS",
"matchCount": "2",
"organizationDetailsList": [
{
"organizationDetails": {
"organizationID": "xxxxx",
"organizationName": "xxxx",
"parentOpCoName": "yyyy",
"registeredEmailID": "zzzz",
"registeredPhoneNo": "xxxx"
}
},
{
"organizationDetails": {
"organizationID": "xxxxx",
"organizationName": "xxxx",
"parentOpCoName": "yyyy",
"registeredEmailID": "zzzz",
"registeredPhoneNo": "xxxx"
}
}
]
};
// ----- getting the header
var headHTML = "<thead>";
var html = "<tbody>";
// ----- getting the row data
$.each(data.organizationDetailsList, function(i){
html += "<tr>";
if(i === 0)
headHTML += "<tr>";
$.each(data.organizationDetailsList[i].organizationDetails, function(key){
html += "<td>" + data.organizationDetailsList[i].organizationDetails[key] + "</td>";
if(i === 0){
headHTML+= "<th>" + key + "</th>";
}
});
html += "<tr>";
if(i === 0)
headHTML += "</tr></thead>";
});
html += "</tbody>"
$('table').append(headHTML, html);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
</table>
&#13;
我已经采用了您的示例JSON数据&amp;从中创建了一个表格。 我使用 $。每个无论你使用什么&amp;刚刚创建了该循环中的html,并将附加添加到表中。
当 i 值 0 时, $。每个也会创建标题。