Jquery 每个 Json 对象

标签 jquery json

我有一个从服务返回的结果集,它为我提供了以下 json

{
    "ThreadCount":61,
    "GroupList":[
        {"userName":"KLT","count":2687},
        {"userName":"KCameron","count":718},
        {"userName":"IRG","count":156},{"userName":"JLB","count":123},{"userName":"HML","count":121},{"userName":"SMN","count":99},{"userName":"TBridges","count":68},{"userName":"ARF","count":65},{"userName":"MarkGreenway","count":61},{"userName":"SMC","count":55},{"userName":"EMD","count":52},{"userName":"PKP","count":41},{"userName":"KBounds","count":36},{"userName":"MAN","count":33},{"userName":"LAC","count":17},{"userName":"EPS","count":17},{"userName":"CAN","count":7},{"userName":"MAJ","count":3},{"userName":"CPC","count":2}]
}

我想使用 Jquery(或 javascript 将 threadCount 放入一个 div 中,并将用户名和编号添加到表中

success: function(result) {
    $("#Unfiled").html(result.ThreadCount);
    $.each(result.GroupList, function(user) {
        $('#myTable > tbody').append(
            '<tr><td>'
            + user.userName
            + '</td><td>'
            + user.count +
            '</td></tr>'
        );
    });
}

由于某种原因,我的 table 上没有任何东西......

顺便说一句,我的 HTML 在这里:

 <table>
    <tr>
        <td>
            Unfiled Emails:
        </td>
        <td id="Unfiled">
            -1
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <table id="myTable" border="2" cellpadding="3" cellspacing="3">
            </table>
        </td>
    </tr>
</table>

我知道我错过了一些简单的东西......

感谢您提前提供的帮助

最佳答案

在提供给 each 的函数中,this 指的是当前元素。试试这个:

$.each(result.GroupList, function() {
    $('#myTable > tbody').append(
        '<tr><td>'
        + this.userName
        + '</td><td>'
        + this.count +
        '</td></tr>'
    );
});

如果这对您不起作用,可能与此有关:$('#myTable > tbody'),考虑到没有tbody 元素。我相信 Internet Explorer 会自动创建一个,但其他浏览器不会。检查$.support.tbody查看浏览器是否为您执行此操作。

关于Jquery 每个 Json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1569372/

相关文章:

javascript - 使用两个值作为一个值的键

java - 如何使用 jackson 序列化 transient 场?

javascript - 如何使用 PostgreSQL 对 JSON 中的日期使用比较运算符(如 BETWEEN 子句)?

javascript - Jquery UI 模态对话框,将焦点设置在第一个表单元素上

c# - ASP :HiddenField get value from JavaScript Variable upon Button Click

javascript - 焦点改变时不隐藏下拉菜单

java - 基于 Enum 的 Jackson 多态反序列化

javascript - 从多个嵌套位置删除没有内容的类的 div

javascript - 当 $.each 和 array.splice(i) 放在一起时,JQuery 处理数组超出索引错误

java - 使用python从java应用程序中读取json