1

这适用于 XML

$(document).ready(function() {
            $.ajax({
                type: "GET",
                url: "http://api.soundcloud.com/users/123/playlists.xml?client_id=ID",
                dataType: "xml",
                success: parse
            });
        });

        function parse(xml) {
           $(xml).find("playlists").each(function(){
                        //var title = $(this).find('title').text();
                        $("#catTitle").append($(this).text()+ "<br />"); 
                    });
        } 
        ​

但是当我把它改成这个时,它就不起作用了。我在之后放了一个 alert() $(json).find("playlists").each(function(){,它永远不会被调用。有什么想法吗?

$(document).ready(function() {
        $.ajax({
            type: "GET",
            url: "http://api.soundcloud.com/users/123/playlists.json?client_id=ID",
            dataType: "json",
            success: parse
        });
    });

    function parse(json) {
       $(json).find("playlists").each(function(){
                    //var title = $(this).find('title').text();
                    $("#catTitle").append($(this).text()+ "<br />"); 
                });
    } 
​
4

2 回答 2

4

这不是 jQuery 旨在做的事情。如果要使用 JSON 定义的对象或数组,请直接使用该对象或数组。

于 2013-01-04T16:22:53.450 回答
0

尝试这个:

function parse(json) {
   $.each(json.playlists,function(idx,el){
       $("#catTitle").append(el+ "<br />"); 
   });
}
于 2013-01-04T16:24:40.067 回答