jQuery:处理多个JSON数据

时间:2016-01-05 20:38:05

标签: jquery json

我的代码:

$.ajax({
    url: 'online.php',
    success: function(data) {
        var userOnline = $.parseJSON(data);
        $('#usersOnline').append($('<div id="usersOnlineUser">').text(userOnline['username']).append($('<span class="badge">').text('aktiv')));
    }
});

JSON:

{"username":"Aarivex","active":"1"}

工作正常。但我如何处理多个JSON数据,如

{"username":"Aarivex","active":"1"}{"username":"Aarivex2","active":"1"}

1 个答案:

答案 0 :(得分:1)

如果我们假设您的数据是:

var data = [{"username":"Aarivex","active":"1"}, {"username":"Aarivex2","active":"1"}];

您的成功功能变为:

success: function(data) {
   $.each(data, function(index, element) {
      $('#usersOnline').append($('').text(element['username']).append($('').text('aktiv')));
   });
}