简体   繁体   中英

JSON multi dimensional array not displaying ('array is not defined')

I made some edits to my returned JSON array and now it broke the code. Essentially, all I did was turn it into a multi-dimensional array (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); 
            } ...

Returned JSON data:

{"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"}]}

In the FOR loop in the JS code, data[threads] is undefined? Is there a reason data[threads][i] wouldn't work?

You don't have any variable named threads .

You mean data.threads .

data.threads is an array with only one cell (which holds an object)

don't use data[threads] : you can user either data.threads or data["threads"] with quotes

Use

data.threads

or

data["threads"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM