0

ajax関数で取得したJSONデータがあります。次のような各関数を使用して、このデータをループします。

                    $.each(data, function (index, trip) {
                    // trip contains data
                    console.log(trip);
                    
                    content += '<article class="tripPreview">';
                    content += '<span class="tripTitle">'+trip.title+ '</span>';
                    content += '<div class="tripOverlay">'+trip.description+ '</div>';
                    content += '<img src="public/uploads/tripphoto/'+ trip.id +'/'+trip.tripphotos.filename+'">';   
                    content += '</article>';        
                    
                });

どうすれば連絡できtripphotos > filename > thumb > urlますか?

JSON:

{"created_at":"2012-12-06T13:02:03Z","description":"test","id":11,"province":"Friesland","range_high":null,"range_low":null,"start_city":"Leeuwarden","title":"test","updated_at":"2012-12-06T13:02:06Z","user_id":1,"views":1,"categories":[{"ar_association_key_name":"4","created_at":"2012-12-04T13:12:43Z","id":2,"name":"Urban","updated_at":"2012-12-04T13:12:43Z"}],"tripphotos":[{"created_at":"2012-12-06T13:02:05Z","filename":{"url":"/uploads/tripphoto/filename/2/1280x1024-colour-wood-flip.jpg","thumb":{"url":"/uploads/tripphoto/filename/2/thumb_1280x1024-colour-wood-flip.jpg"}},"id":2,"trip_id":11,"updated_at":"2012-12-06T13:02:05Z"}]}

4

1 に答える 1

1

JSONからは配列のように見えるtripphotosので、別のものが必要になりますeach

$.each(data, function (index, trip) {
   ...
   $.each(trip.tripphotos,function(index2,tripphoto){
      console.log(tripphoto.filename.thumb.url);
   });
});

あなたがこれまでに1つだけ存在することを知っていない限り(または、あなたは最初のものだけを求めています):

$.each(data, function (index, trip) {
    ...
    console.log(trip.tripphotos[0].filename.thumb.url);

});
于 2012-12-07T11:36:40.590 に答える