0

I have JSON data which I get with an ajax function, I loop through this data using a each function like this :

                    $.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>';        
                    
                });

How do I reach 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
  • Can you clarify what you're trying to do, and why your current approach isn't working? Also, can you post the original JSON string that you're parsing? What you've posted looks like it's copied out of the developer tools console of a browser after calling console.log on the object? The trouble is that doesn't expand objects to show you all their properties. Commented Dec 7, 2012 at 0:24
  • trip.tripphotos[0].filename.thumb.url That's an array ? Commented Dec 7, 2012 at 0:28
  • Change console.log(trip) to console.log(JSON.stringify(trip)) and paste what you get, so we can know what the structure of your trip object is. Commented Dec 7, 2012 at 1:52
  • Ive added the whole object stringyfied, Iam trying to show the image to the user in the each like this <img src="filenamefromjsonobjecthere/> this doesn't work in the each trip.tripphotos.filename.url Commented Dec 7, 2012 at 11:25

1 Answer 1

1

From your JSON, it looks like tripphotos is an array, so you'll need another each:

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

Unless you know there will only ever be one (or, you're after just the first):

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

});
Sign up to request clarification or add additional context in comments.

Comments

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.