2013-01-04 16 views
1

这正常工作与XMLjQuery的.find与XML,但不是JSON工作

$(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 />"); 
        }); 
     } 
     ​ 

但是当我改变这一点,这是行不通的。我在$(json).find("playlists").each(function(){之后放了一个alert(),它永远不会被调用。有什么想法吗?

$(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 />"); 
       }); 
    } 
​ 
+2

无法搜索'json'与jQuery,这是一个JavaScript对象或数组。像使用JavaScript对象或数组一样使用它。 –

+0

是的,我有一个想法,你为什么期望它的工作?它也不适用于XML,它只是发生XML有足够的类似于HTML的结构来欺骗JQuery – musefan

+0

json没有格式化为使用.find()方法使用标记 –

回答

0

试试这个:

function parse(json) { 
    $.each(json.playlists,function(idx,el){ 
     $("#catTitle").append(el+ "<br />"); 
    }); 
} 
+0

谢谢 - 这就是我最终做了'var items = []; var content = null; $ .getJSON('http://api.soundcloud.com/users/123/playlists.json?client_id= [客户端ID]',功能(数据){ \t content = data; }); 功能解析(JSON){$ 。每个(内容,功能(索引,项目){ 如果(item.genre ==类型){ //其余的功能在这里 }} } ;' – frankie

+0

我很乐意帮助你。 –

4

这不是jQuery设计的目的。如果您想使用由JSON定义的对象或数组,请直接使用该对象或数组。

+0

我有点困惑。为什么它对这个人有效:http://stackoverflow.com/questions/1241068/help-needed-retrieving-json-data-with-jquery – frankie

+0

@frankie它没有。 –