迭代由getJSON()返回的jsonarray

时间:2017-03-05 14:13:06

标签: javascript jquery json ajax

如何迭代JSON数组?控制台输出:

  

未捕获的TypeError:无法读取属性' index1'未定义的       在Object.success(index.html:29)

变量data中的格式:

[{"index1":2325,"proto_name":"SSH","byte_count":8523026},
 {"index1":2325,"proto_name":"HTTP","byte_count":2123844},
 {"index1":2325,"proto_name":"ab", "byte_count":232}
]

JS代码:

<script type="text/javascript">

    function LoadTraffic() {
        $(function() {
            var url = "traffic";

        $.getJSON( url,
            function(data){
                for (var i = 0; i <data.length; i++) {
                    var result = data[i];
                    var $tr = $('<tr/>');
                    $tr.append($('<td/>').html(result.index1));
                    $tr.append($('<td/>').html(result.proto_name));
                    $tr.append($('<td/>').html(result.ts));
                    $('.list-order tr:last').before($tr);
                }
           });
       });
    }

    setInterval( LoadTraffic, 5000 ); <!--1s-->

</script>

0 个答案:

没有答案