2013-01-04 32 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它沒有。 –