在jQuery $ .each中访问JSON中的多个对象/数组

时间:2013-11-06 15:51:32

标签: javascript php jquery mysql json

我正在尝试构建一个简单的页面,通过JSON和jQuery查看保存在mysql数据库中的消息。

我的JSON是

{"unread_msgs":[{"id":"6","title":"33333","user1":"19","timestamp":"1383747146","client_id":"Generic"},{"id":"5","title":"42142","user1":"19","timestamp":"1383740864","client_id":"Generic"}],"read_msgs":[{"id":"4","title":"test to addnrow","user1":"19","timestamp":"1383676647","client_id":"Generic"},{"id":"2","title":"kll","user1":"19","timestamp":"1383675548","client_id":"Generic"},{"id":"1","title":"jkjljklj","user1":"19","timestamp":"1382539982","client_id":"Generic"}],"urm_t":2,"rm_t":3}

我的JS

<script type="text/javascript">
function GetClientMsgs () {
    $.post("assets/server/client_msgs.php",{ client_id:local_client_id } ,function(data)
    {           
        var MsgData = $.parseJSON(data);
        console.log("Msg json data parsed");

        $.each(MsgData, function(key, value)
        {
          console.log(value);
            $('#unread_row').append('<td><a href="#read_pm" onClick="ReadMsg('+value.unread_msgs.id+'); return false;">'+value.unread_msgs.title+'</a></td><td><a href="#profile">'+value.unread_msgs.studioname+'</a></td><td>'+value.unread_msgs.timestamp+'</td><td><a href="#delete_msg" onClick="DeleteMsg('value.unread_msgs.id+'); return false;">X</a></td>');

            $('#read_row').append('<td><a href="#read_pm" onClick="ReadMsg('+value.read_msgs.id+'); return false;">'+value.read_msgs.title+'</a></td><td><a href="#profile">'+value.read_msgs.studioname+'</a></td><td>'+value.read_msgs.timestamp+'</td><td><a href="#delete_msg" onClick="DeleteMsg('+value.read_msgs.id+'); return false;">X</a></td>');
        });
    });
}                   
</script>        

PHP

$msgs = array('unread_msgs' => $dn1s, 'read_msgs' => $dn2s, 'urm_t' => $unread_msgs, 'rm_t' => $read_msgs);

$json = json_encode($msgs);

我正在尝试发布返回的值,例如unread_msgs.title或.id,并且无法访问任何对象。我在这里搜索过,找不到我的结构特有的东西。非常感谢。

2 个答案:

答案 0 :(得分:1)

我会循环遍历未读和读取结果,如下所示:

    $.each(MsgData.unread_msgs, function(key, value)
    {
      // code append to unread row
      console.log(value.id, value.title, "etc");
    });

    $.each(MsgData.read_msgs, function(key, value)
    {
      // append to append to read row
      console.log(value.id, value.title, "etc");
    });

将json粘贴到http://jsonformatter.curiousconcept.com/等格式化程序中可以帮助您查看其结构。

答案 1 :(得分:0)

在这一行:

$('#unread_row').append('<td><a href="#read_pm" onClick="ReadMsg('+value.unread_msgs.id+'); return false;">'+value.unread_msgs.title+'</a></td><td><a href="#profile">'+value.unread_msgs.studioname+'</a></td><td>'+value.unread_msgs.timestamp+'</td><td><a href="#delete_msg" onClick="DeleteMsg('value.unread_msgs.id+'); return false;">X</a></td>');

您正在访问value变量作为对象,而它实际上是一个数组。

要在$.each循环中访问第一个未读邮件ID,您可以:value[0].id

如果您要获取未读和已读消息的列表,请尝试循环播放它们。

$.each(MsgData.unread_msgs, function(index, message) {
    $('#unread_row').append('<td>' + message.id + '</td><td>.......');
});

$.each(MsgData.read_msgs, function(index, message) {
    $('#read_row').append('<td>' + message.id + '</td><td>.......');
});

或者使用嵌套循环:

$.each(MsgData, function(key, value) {
    $.each(value, function(index, message) {
        $('#' + key).append('<td>' + message.id + '</td><td>....);
    });
]);