2014-04-11 37 views

回答

1

你需要通過每個節點及其子節點遞歸循環,並檢查他們的鍵名。有這樣的圖書館可以幫助你。或者,您可以使用xpath for JSON

1

你可以試試這樣:

$.getJSON('http://www.reddit.com/r/AskReddit/comments/22rx5c.json', function(json){ 
    result = []; 
    recurse('body', json); 
    console.log(result); 
}); 

function recurse(toFind, json){ 
    for(var key in json){ 
     if(typeof json[key] == 'object'){ 
      recurse(toFind, json[key]); 
     } 
     if(key == toFind){ 
      result[result.length] = json[key]; 
     } 
    } 
}; 

在這裏你可以找到JSFiddle

相關問題