0

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 [email protected]\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?

1
  • 1
    That's not a multidimensional array. Commented May 11, 2012 at 19:25

3 Answers 3

4

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

4

You don't have any variable named threads.

You mean data.threads.

3
  • how do i get the length of data.threads? Also, how would i display the the i'th iteration of the thread when there are more than 1? Commented May 11, 2012 at 19:31
  • @RickyMason: Just like any other array. Commented May 11, 2012 at 19:33
  • i needed it with quotations, data['threads'][i] worked wonders. Commented May 11, 2012 at 19:44
3

Use

data.threads

or

data["threads"]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.