【发布时间】:2017-04-10 17:23:06
【问题描述】:
在 ajax 调用之后,我在数据中有 json 对象
[
{
"_id": {
"$id": "58d8e2831d7859e80d000033"
},
"broadcast_id": 70,
"studentList": "",
"employeeList": "999",
"mailTitle": "adsf",
"broadcastMessage": "dsfsdf dsd fgd",
"emailSent": "0",
"userdetails": []
},
{
"_id": {
"$id": "58d8eaba1d7859c81300002e"
},
"broadcast_id": 72,
"studentList": "",
"employeeList": "999|788",
"mailTitle": "Hekjh",
"broadcastMessage": "hhyky jk",
"emailSent": "0",
"userdetails": []
},
{
"_id": {
"$id": "58dde8ed1d78597011000029"
},
"user_id": 1,
"broadcast_id": 76,
"studentList": "",
"employeeList": "999|788",
"mailTitle": "Hello",
"broadcastMessage": "How are u ",
"emailSent": "0",
"dateSent": "31/03/2017",
"userdetails": [
{
"_id": {
"$id": "568f95dc99fbadb016000029"
},
"uid": 1,
"username": "test",
"password": "LeLafe#7861",
"email_id": "sales@lelafe.com",
"creation_date": "",
"role": "admin",
"is_enabled": 1
}
]
}
]
现在我正在尝试解析嵌套的 json 属性 userdetails 以便在表格中扮演角色并将其显示。我试过以下几行
$.ajax({
url: 'index.php?action=fetchBroadcastedMessageList',
type: 'POST',
dataType: 'JSON',
data: { usertype: usertype },
success: function(data) {
for (var i in data) {
$("#broadcastedmessagelist").append('<tr>' +
'<td style="text-align: center;">' +
'' + data[i].userdetails["role"] + '' +
'</td>'...
);
}
});
}
data[i].userdetails["role"] 行没有检索到对应的角色
只要有可用的用户详细信息,就值。请帮我 !!!
【问题讨论】:
-
userdetails 包含一个数组,你必须使用 data[i].userdetails[0]["role"] 才能做你想做的事
-
data[i].userdetails 是一个数组
-
你检查我的答案了吗?
标签: javascript jquery json ajax