0

today, i learned the basics of Json and its parsing through Jquery. I know my question is futile but i am stuck and unable to get a way out.

the following code doesn't work....but i guess i did everything ok

<html>
<head>
<div id="placeholder"></div>
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>

<script>
$(document).ready(function(){

$.getJSON("http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=2d34860785184c2e3458de2bc437ecfc&tags=rose&format=json&nojsoncallback=1&api_sig=b7303e1d0d8a9fbb1159404ca7927e98",function(data){
var output="<ul>";
for (var i in data.photos) {
    output+="<li>" + data.photos.photo[i].id+"</li>";


    if (i === 3){
    return false;
}
}
output+="</ul>";

document.getElementById("placeholder").innerHTML=output;
})
})
</script>
</head>

<body>


</body>

</html>
4

1 回答 1

2

您在JSON中的索引错误,您需要进行以下更改。

演示:http: //jsfiddle.net/ALJkV/

$(document).ready(function () {

    $.getJSON("http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=2d34860785184c2e3458de2bc437ecfc&tags=rose&format=json&nojsoncallback=1&api_sig=b7303e1d0d8a9fbb1159404ca7927e98", function (data) {
        var output = "<ul>";
        for (var i in data.photos.photo) { // The photo index is what you need;

            // If you want to skip 3
            if (i == 3) continue;
            // Or if you want to stop at 3 
            if(i == 3) break;

            output += "<li>" + data.photos.photo[i].id + "</li>";
        }
        output += "</ul>";

        document.getElementById("placeholder").innerHTML = output;
    });
});

继续:https://developer.mozilla.org/en...

于 2013-08-03T13:05:25.687 回答