2013-06-21 156 views
0

迭代由於我是相當新的JSON和jQuery我掙扎過一個對象,我SOFAR能夠用下面的代碼加載到迭代:通過JSON對象

$.ajax({ 
    url: source, 
    dataType: 'json', 
    success: function(obj) { 
     // Laat een succes-melding zien wanneer de data goed ingeladen is 
     console.log("SUCCESS"); 
    }, 
    error: function(obj) { 
     // Laat een error-melding zien wanneer de data niet goed ingeladen is 
     alert("ERROR"); 
    }, 
    complete: function(obj) { 
     console.log(obj);   
    } 
}); 

不幸的是結構JSON的是我的口味有點複雜,但因爲它是一個外部源(通過YQL)沒什麼,可將其改爲:

{ 
    "query": { 
    "count": 1, 
    "created": "2013-06-21T11:42:57Z", 
    "lang": "en-US", 
    "diagnostics": { 
     "publiclyCallable": "true", 
     "url": { 
     "execution-start-time": "1", 
     "execution-stop-time": "1868", 
     "execution-time": "1867", 
     "proxy": "DEFAULT", 
     "content": "contentsource" 
     }, 
     "user-time": "1868", 
     "service-time": "1867", 
     "build-version": "37740" 
    }, 
    "results": { 
     "viewentries": { 
     "toplevelentries": "0", 
     "viewentry": [ 
      { 
      "date": "2013-06-11", 
      "noteid": "A2FA", 
      "position": "1", 
      "siblings": "0", 
      "unid": "15D7F7A15C0DAE", 
      "entrydata": [ 
       { 
       "columnnumber": "0", 
       "name": "Datum_van_instantie", 
       "text": "2013-06-11" 
       }, 
       { 
       "columnnumber": "1", 
       "name": "kopregel", 
       "text": "Title of newsitem 1" 
       }, 
       { 
       "columnnumber": "2", 
       "name": "tussenkopje", 
       "text": "Introduction text of newsitem 1" 
       }, 
       { 
       "columnnumber": "3", 
       "name": "broodtekst", 
       "text": "Content of newsitem 1" 
       }, 
       { 
       "columnnumber": "4", 
       "name": "imagetag", 
       "text": "Image URL of newsitem 1" 
       } 
      ] 
      }, 
      { 
      "date": "2013-05-29", 
      "noteid": "A2F6", 
      "position": "2", 
      "siblings": "0", 
      "unid": "9237CD3822ED8DD", 
      "entrydata": [ 
       { 
       "columnnumber": "0", 
       "name": "Datum_van_instantie", 
       "text": "2013-05-29" 
       }, 
       { 
       "columnnumber": "1", 
       "name": "kopregel", 
       "text": "Title of newsitem 2" 
       }, 
       { 
       "columnnumber": "2", 
       "name": "tussenkopje", 
       "text": "Introduction text of newsitem 2" 
       }, 
       { 
       "columnnumber": "3", 
       "name": "broodtekst", 
       "text": "Content of newsitem 2" 
       }, 
       { 
       "columnnumber": "4", 
       "name": "imagetag", 
       "text": "Image URL of newsitem 2" 
       } 
      ] 
      } 
     ] 
     } 
    } 
    } 
} 

我想完成的事情是打印類似的東西跟隨我的HTML文件中的元素:

Title of newsitem 1 
Title of newsitem 2 

我肯定從那時起我很高興從文件中提取剩餘的項目。用我想要的東西對我來說是有道理的,在各種obj.viewentry [0]選項上使用$ .each循環,我得到一個未定義的錯誤。當我嘗試訪問更具體的例如

$.each(obj.viewentry, function(index, value) { 
    console.log(this.entrydata[0]);    
}); 

我得到的回報是一個jQuery錯誤,TypeError:e未定義。雖然我確信最後的代碼也是'jQuery for Dummies',但我今天早上搜索的其他答案並沒有給我更多的清晰。

回答

0

嘗試使用這樣的:

$.each(obj.query.results.viewentries.viewentry, function (index, value){ 
    $.each(value.entrydata, function (index, value) { 
     if (value.name == "kopregel") console.log(value.text); 
    }); 
}); 

這是工作:http://jsfiddle.net/8tKYd/2/

0

沒有理由使用jQuery這個(除了AJAX部分):

for (var viewEntryKey in obj.query.results.viewentries.viewentry) { 
    for (var i = 0; i < obj.query.results.viewentries.viewentry[viewEntryKey].entrydata.length; i++) { 
     if (obj.query.results.viewentries.viewentry[viewEntryKey].entrydata[i].name == 'kopregel') { 
      console.log(obj.query.results.viewentries.viewentry[viewEntryKey].entrydata[i].text); 
     } 
    } 
} 

輸出:

Title of newsitem 1
Title of newsitem 2