That's what I thought too. The only code example I found (one based on the
NYTimes article API) used the for-loop approach, so with the evidence of
your experience, I concluded it was the "correct" approach.
http://notetodogself.blogspot.com/2009/02/using-jquery-with-nyt-json-api.html
It still seems wrong to have to loop on the results container since there is
only ONE occurrence of it. It is the book_details element that repeats.
Tom
On Thu, Sep 29, 2011 at 9:21 PM, Nate Hill <[log in to unmask]> wrote:
> Thanks Tom. I guess I just don't understand jQuery $.each(), I thought
> with
> that I didn't need a javascript for loop- that each() would just plow
> through and return everything.
> Cheers, I appreciate the help.
> N
>
> On Thu, Sep 29, 2011 at 6:14 PM, Tom Keays <[log in to unmask]> wrote:
>
> > I haven't quite figured out why the each is not behaving as expected, but
> > this worked, returning all 20 titles delivered by the JSON.
> >
> > I expected the each() to parse down from the single results container, so
> > why book_details has to be referenced by incrementing the results array
> (or
> > is it an object?) seems wrong.
> >
> > <!DOCTYPE html>
> > <html>
> > <head>
> > <meta charset="iso-8859-1">
> > <title> NY Times Hardcover Fiction Bestsellers </title>
> > <script type="text/javascript" src="
> > http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script>
> > <script type="text/javascript">
> > $(document).ready(function(){
> > //json request to new york times via proxy
> > $.getJSON('
> http://www.sjpl.org/otherscripts/callback.php?callback=
> > ?
> > ',
> > function(data){
> > for(var i = 0; i < 20; i++){
> > $.each(data.results[i].book_details, function(n,item){
> > var bookTitle = item.title;
> > $('#container').append('<p>'+bookTitle+'</p>');
> > });
> > };
> > });
> > });
> > </script>
> >
> > </head>
> > <body>
> >
> > <div id="container"></div>
> >
> > </body>
> > </html>
> >
> >
> >
> > On Thu, Sep 29, 2011 at 5:33 PM, Nate Hill <[log in to unmask]>
> > wrote:
> >
> > > *Here's my problem:*
> > > I can only return the book title and the isbn by specifying which one
> in
> > > the
> > > array I want, so I'm using: data.results[0].book_details to pull the
> > first
> > > title.
> > > Something is wrong with the way I'm using $.each()
> > >
> >
>
>
>
> --
> Nate Hill
> [log in to unmask]
> http://www.natehill.net
>
|