Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to get the simple table format from the json(results array) below, I wish to use jquery .each. I tried this code but how to get the object? I tried v.subject but doesn't work.

$.getJSON( "http://127.0.0.1:8000/ajax/list/?page=1&format=json", function( data ) {
   $.each( data, function() {
      $.each( this, function( i, v ) {
         console.log( v );
      });
   });
});

result of console.log(v)

 Object { subject="ronald birthday", date="2014-06-13", time_start="11:17 PM"}
 Object { subject="ronald birthday", date="2014-06-19", time_start="7:17 PM"}

This is the format table

      <table >

        <tr>
         <td> <a href="#"> Rita Birthday</a> </td>
         <td> <a href="#"> Oct. 18, 2014</a> </td>
         <td> <a href="#"> 11:27 PM</a> </td>
        </tr>

       </table>

This is the json but only the results array should be in the table:

{"count": 18, "next": "http://www.nothingonlytest.com/ajax/list/?page=2&format=json", "previous": null, "results": [{"subject": "ronald birthday", "date": "2014-06-13", "time_start": "11:17 PM"}, {"subject": "ronald birthday", "date": "2014-06-19", "time_start": "7:17 PM"}, {"subject": "Rita Birthday", "date": "2014-10-18", "time_start": "11:27 PM"}, {"subject": "tt", "date": "2014-06-27", "time_start": "10:31 PM"}, {"subject": "tt", "date": "2014-06-13", "time_start": "10:31 PM"}, {"subject": "group event", "date": "2014-06-14", "time_start": "3:31 AM"}, {"subject": "test message", "date": "2014-06-17", "time_start": "3:32 AM"}, {"subject": "test event message", "date": "2014-06-14", "time_start": "3:34 AM"}, {"subject": "fd", "date": "2014-06-20", "time_start": "3:36 AM"}, {"subject": "fdf", "date": "2014-06-14", "time_start": "3:38 AM"}]}

share|improve this question

closed as off-topic by JK., Brad Koch, Achrome, Ivan Ferić, Scott Jul 11 '14 at 8:58

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – JK., Brad Koch, Achrome, Ivan Ferić, Scott
If this question can be reworded to fit the rules in the help center, please edit the question.

    
the data that needs to be iterated is data.results –  Yuliam Chandra Jul 11 '14 at 4:04

1 Answer 1

up vote 0 down vote accepted

Here is one way to do it.

  1. You need to get the table object using jquery selector.
  2. Then you need to loop the results property of the json data using for keyword.
  3. For each object of the result, get the property of subject, date and time_start using property accessor.
  4. Create a row string such as "<tr><td>" + val1 + "</td><td>" + val2 + "</td><td>" + val3 + "</td></tr>" and append it to the table object using jquery append.

UPDATE

You need to loop the results property instead.

$.getJSON("http://127.0.0.1:8000/ajax/list/?page=1&format=json", function( data ) {
   $.each(data.results, function() {
      $.each( this, function( i, v ) {
         console.log( v );
      });
   });
});
share|improve this answer
    
I really like to use .each than for loop and can you please post an example. –  user2387135 Jul 11 '14 at 3:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.