我有一个JSON
结构
[
{
"id":"3",
"0":"3",
"name":"What ever",
"1":"What ever",
"email":"dd@dd.dd",
"2":"dd@dd.dd",
"mobile":"7777777",
"3":"7777777",
"address":"Bikrom Pur",
"4":"Bikrom Pur"
}
]
当我使用以下jQuery函数通过表解析这些数据时,一切都很正常:
function renderUserList(jsonData) {
var table = '<table width="600" cellpadding="5" class="table table-hover table-bordered"><thead><tr><th scope="col">Name</th><th scope="col">Email</th><th scope="col">Mobile</th><th scope="col">Address</th><th scope="col"></th></tr></thead><tbody>';
$.each( jsonData, function( index, posts){
table += '<tr>';
table += '<td class="edit" field="name" user_id="'+posts.id+'">'+posts.name+'</td>';
table += '<td class="edit" field="email" user_id="'+posts.id+'">'+posts.email+'</td>';
table += '<td class="edit" field="mobile" user_id="'+posts.id+'">'+posts.mobile+'</td>';
table += '<td class="edit" field="address" user_id="'+posts.id+'">'+posts.address+'</td>';
table += '<td><a href="javascript:void(0);" user_id="'+posts.id+'" class="delete_confirm btn btn-danger"><i class="icon-remove icon-white"></i></a></td>';
table += '</tr>';
});
table += '</tbody></table>';
$('div#content').html(table);
}
我更新了服务器端脚本以生成这个JSON结构。
{
"success":1,
"message":"Post Available!",
"posts":[
{
"id":"39",
"name":"Ahmed",
"email":"sabsab58@gmail.com",
"mobile":"778899",
"address":"41122333"
}
]
}
在更新JSON结构之后,我无法再次通过表解析数据,我在表的字段中得到的全部内容都是undefined
。在JSON和jQuery方面,我是一个安静的初学者。
为了使应用程序像以前一样工作,我应该对jQuery函数进行什么更改,以及如何在jQuery上获得内部JSON数组?
发布于 2014-12-30 07:58:36
试着绕过去
jsonData.posts
比如:
$.each( jsonData.posts, function( index, posts){
table += '<tr>';
table += '<td class="edit" field="name" user_id="'+posts.id+'">'+posts.name+'</td>';
table += '<td class="edit" field="email" user_id="'+posts.id+'">'+posts.email+'</td>';
table += '<td class="edit" field="mobile" user_id="'+posts.id+'">'+posts.mobile+'</td>';
table += '<td class="edit" field="address" user_id="'+posts.id+'">'+posts.address+'</td>';
table += '<td><a href="javascript:void(0);" user_id="'+posts.id+'" class="delete_confirm btn btn-danger"><i class="icon-remove icon-white"></i></a></td>';
table += '</tr>';
});
发布于 2014-12-30 08:02:11
将$.each( jsonData, ...
更改为$.each( jsonData.posts, ...
因为您正在遍历jsonData.posts
https://stackoverflow.com/questions/27708525
复制