javascript - 嵌套 JSON 数组对象代码不起作用

标签 javascript jquery json ajax

在 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"]没有检索到相应的 Angular 色 只要有可用的用户详细信息,就具有值(value)。请帮我 !!!

最佳答案

你也可以这样使用

 $.ajax({
       url: 'index.php?action=fetchBroadcastedMessageList',
       type: 'POST',
       dataType: 'JSON',
       data: {usertype: usertype},
       success: function (data) {
            $.each(data , function(key,value)) {
                       $("#broadcastedmessagelist").append('<tr>' +
                              '<td style="text-align: center;">' +
                              '' + value.userdetails[0]["role"] + '' +
                              '</td>'...
           );
           }
       });  
      }

关于javascript - 嵌套 JSON 数组对象代码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43247962/

相关文章:

javascript - 如何在 Sails.js 中将 Json 验证消息转换为字符串消息?

javascript - 单击 "continue"按钮 Javascript 时单选按钮验证

json - 保存 json 响应并将其解析为 swift 对象

javascript - Chrome 不允许选项卡阻止默认

JavaScript 日期解析

java - Spring Rest 返回带有特定 http 响应代码的 JSON 响应

c# - 在反序列化过程中有选择地转义字符串中的 HTML

javascript - 两个 SVG 元素接触时的事件

javascript - 函数返回带参数的函数

javascript - 下拉值不发布