0

I was having trouble trying to make this code work. I really don't see anything wrong with the javascript. When I debug, I don't find errors either.

<head>
<style type="text/css">
</style>
<script src="http://code.jquery.com/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
 $(document).ready(function() {
$('#Presentation').click(function() {
var jsonloc = "ppt.json";
        $.when($.getJSON(jsonloc)).then(function(info){
        $('#header').empty();
            $.each(info.slides, function(entryIndex, entry){
            var html = '<div class="info">';                      
            html += '<h3>' + entry['title'] + '</h3>';
            html += '<div class="author">' + entry['author'] + '</div>';                    
                if(entry['slides']){
                    $.each(entry['slides'],function(slideIndex, slides){
                        html += '<h3>' + slides['Slide'] + '<h3>';                                          
                        html += '<div class="header">' + slides['header'] + '</div>';                          
                        }); 
                if(slides['Content']){
                html += '<div class="Content">';                                            
                html += '<ol>';
                    $.each(slides['content'],function(contentIndex, content){
                        html += '<li>' + content + '</li>';                        
                            }); 
                        html += '</ol>';                        
                        html += '</div>';                               
                    };                                  
                $('#header').append(html);
            };                         
        });
        return false;
    });
    });

});
</script>
</head>
<body>

<a href="#" id="Presentation">ppt presentation</a>
<div id="header">
</div>
</body>

here is the JSON:

{
    "title": "presentation",
    "date_created": "",
    "last_modified": "",
    "author": "By: Someone online",
    "slides": [
        {
            "Slide": "1",
            "header": "first header",
            "src": "ssss.jpg",
            "Content": "dddddddddddddddddddddddddddddddddddd",
            "Content": "dddddddddddddddddddddddddddddddddddd",
            "Content": "dddddddddddddddddddddddddddddddddddd"
        },
        {
            "Slide2": "2",
            "header2": "header 2",
            "src2": null,
            "Content": "dddddddddddddddddddddddddddddddddddd",
            "Content": "dddddddddddddddddddddddddddddddddddd",
            "Content": "dddddddddddddddddddddddddddddddddddd"

        },
        {
            "Slide3": "3",
            "header3": "header3",
            "src3": "sdfdsf.jpg",
            "Content": "dddddddddddddddddddddddddddddddddddd",
            "Content": "dddddddddddddddddddddddddddddddddddd",
            "Content": "dddddddddddddddddddddddddddddddddddd"
        }
    ]
}

I really want to make this work and don't want to use other methods such as jquery templates. Is there anything that jumps out?

3
  • 1
    I don't think that's possible to have a json file with the same keys multiple times, because you won't be able to retrieve the value for Content` Commented Jan 16, 2013 at 2:04
  • I was trying different codes and methods. I have the JSON fixed now. Commented Jan 16, 2013 at 2:24
  • 1
    Please edit your question with the relevant json Commented Jan 16, 2013 at 2:38

1 Answer 1

1

You're closing .each too soon :

$.each(entry['slides'],function(slideIndex, slides){
    html += '<h3>' + slides['Slide'] + '<h3>';                                          
    html += '<div class="header">' + slides['header'] + '</div>';                          
 }); 
 if(slides['Content']){ 
 // ...

That way you only access the last value of slides['Content'].

You need to put that if and what it contains into your loop like this :

 $.each(entry['slides'],function(slideIndex, slides){
    html += '<h3>' + slides['Slide'] + '<h3>';                                          
    html += '<div class="header">' + slides['header'] + '</div>';      
    if(slides['Content']){
    // ...

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

3 Comments

Okay, so I corrected that, but still... there is no JSON displayed
Also your json is messed up, why do you have "Slide", "Slide2", "Slide3" ?
I was trying other codes on the JSON so I kept changing it. Ive corrected it now, however, still no luck.

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.