1
votes

This is what I have so far. I'm trying to parse json file and grab img path and width get the return for each record in GETJson json and append to "Gallery" Inside an unordered list with a class named thumbs. So instead of having several lines of code "li img src= path of images" width= " in the html, I could just read json file and populate this.

My json looks like this:

[ { "image":"sliders/img/img1.jpg", "width":400 }, { "image":"sliders/img/img2.jpg", "width":400 }, ]

Here's the Getjson logic.

<script type="text/javascript">
$().ready(function(){
    $.getJSON('fred.json', function(data) {
         //Collection of li elements
         var items = [];

          $.each(data, function(key, val) {
            items.append('<li><img src="' + val.image + '+ '" width="' 
                               + val.width + '" /></li>');
            });

          $('<ul/>', {
            'class': 'thumbs',
            html: items.join('')
          }).appendTo('#Gallery');

          //Once all ul are created call the gallery function
          $('#Gallery').flickrGallery();
    });
});
</script>

Thank You for any help

I incorrectly type the img name in the json file description. I have it spelled out correctly. When I run the html two things happen. The images do not show at all. Everything else appears background etc. for the images does appear. When I do an alert(items) I can see the li populating. Basically, I have a div id=Gallery and and ul inside the div named thumbs_1 with class =thumbs

div id = GALLERY
ul id="thumbs_1" class="thumbs"
li> "img src="images/image_11.jpg" width=600" there are a total of 11 lines like this. Instead of having this harcoded in the HTML I just want to dynamically place the same line of code using GetJson. Then once the DIV is populated
use this
$().ready(function(){ $('#Gallery').flickrGallery({
which calls a specfic function that has been developed already and works to creates a photo gallery. Sorry for any confusion and Thank you for already helping out.

4
image != img . You have "img":"sliders/img/img1.jpg" but then ask val.image. Unless I'm reading the code wrongAleadam
So what is the problem you are getting?CarlosZ
Can you post a link to the flickrGallery plugin you are using? After a quick google it looks like it expects you to pass in a flickr api key and download your images via flickr.Nicky Waites

4 Answers

1
votes

Your json as an extra coma after the last "}" and I'm not actually sure you can ".append" in an array. You could try something like that

var items = '<ul>';

          $.each(data, function(key, val) {
            items += '<li><img src="' + val.image + '+ '" width="' 
                               + val.width + '" /></li>';
            });
          items += '</ul>';

And then append the 'items' var in #gallery like this

$('#gallery').append(items);

But i'm not really sure about what you are trying to do. I assume you tested every bit of the code with "alert()" to see if it runs well.

1
votes

I couldn't get flickrGallery to work without an api key but I've created two examples for you to look through using GalleryView. One using jQuery templates and one without.

Gallery Demo with JSON


Try this in your each loop (or take a look)

$.each(result, function() {
   $("#thumbs_1").append(
       $("<li>").append(
          $("<img>").attr("src", this.image).attr("width", this.width).addClass("thumbs")
       )
   );
});
0
votes

you have an extra concatenation in your .each, and should use items.push instead of items.append (and there's that extra comma at the end of the JSON)...the following works (with your assertion that the gallery creation works)

<script type="text/javascript">
$().ready(function(){
    $.getJSON('fred.json', function(data) {
         //Collection of li elements
         var items = [];

          $.each(data, function(key, val) {
            items.push('<li><img src="' + val.image + '" width="' 
                               + val.width + '" /></li>');
            });

          $('<ul/>', {
            'class': 'thumbs',
            'id':'thumbs_1',
            html: items.join('')
          }).appendTo('#Gallery');

          //Once all ul are created call the gallery function
          $('#Gallery').flickrGallery();
    });
});
</script>
0
votes
<script type="text/javascript">

$.getJSON('test.json',function(data)
{
    alert(JSON.stringify(data));
    $.each(data, function(i,value)
        {
    //  alert(value.NewsCategory.id + ":" + value.NewsCategory.name + " " + value.NewsCategory.organization_id + " " + value.NewsCategory.last_updated);

            var a = value.NewsCategory;
            var news = value.News;
                $.each(news, function(j,details)
                    {
                //  alert(details.id + " " + details.title + " "+ details.date_created + " " + details.body + " " +details.thumb);
                        content+= '<p>'+ "ID : "+ news[j].id + " Date Created : " + news[j].date_created + " Title : " + news[j].title +'</p>';
                        content+= '<a href = #>' + '<img src="' + news[j].thumb + '"/>' + '</a>';
                        content+= '<br/>';
                     });

                $(content).appendTo("#posts");
         });
 });   

/* ]]> */

<body>
    <div class="container">
            <div class="span-24">
                    <div id="posts">
                    </div>
            </div>
    </div>