JSON多维数组不显示('数组未定义')

时间:2012-05-11 19:22:56

标签: javascript ajax json

我对我返回的JSON数组进行了一些编辑,现在它破坏了代码。基本上,我所做的就是把它变成一个多维数组(2?)

JS

$(function() { ...
     ...$.ajax({
            type: "POST",
            url: 'updatefilters',
            dataType: 'json', 
            data: { filters: filters, page: page },
            success: function(data){
                html = "<table class='board'>";
                html += "<table class='board'>";
                html += "   <tr>";
                html += "           <th width='7.5%'>Author</th>";
                html += "           <th width='5%'><img src='../img/board/icons/category_icon.png' alt='category_icon'/></th>";
                html += "           <th width='5%'>Tag</th>";
                html += "           <th width='50%'>Subject</th>";
                html += "           <th width='7.5%'>Replies/Views</th>";
                html += "           <th width='15%'>Last Post</th>";
                html += "  </tr>";
                for (i=0 ; i < data[threads].length ; i++)
                {
                    html += "<tr><td>" + data[threads][i].username + "";
                }
                html += "</table></div>";
                $('#board').html(html); 
            } ...

返回的JSON数据:

{"0":"blurb","filter":["blurb"],"threads":[{"thread_id":"234","owner_id":"3","subject":"Blurb(?) is in Heavy Development!","body":"Please be aware that this site is still in heavy development. What you are viewing here is a prototype, not meant to be seen as a final product. if you have comments or suggestions, please send it to rickymm3@gmail.com\n\nThank You!","timestamp":"2012-05-11 08:02:28","replystamp":"2012-05-11 08:02:28","last_post_id":"3","slug":"234-blurb-is-in-heavy-development","replies_num":"0","views":"1","username":"guest"}]}

在JS代码的FOR循环中,数据[threads]是未定义的?有没有理由数据[threads] [i]不起作用?

3 个答案:

答案 0 :(得分:4)

您没有名为threads的任何变量。

您的意思是data.threads

答案 1 :(得分:4)

data.threads是一个只有一个单元格(包含一个对象)的数组

不要使用data[threads]:您可以使用引号<{1}}或data.threads

答案 2 :(得分:3)

使用

data.threads

data["threads"]