2014-03-01 46 views
0

我想在jQuery中解析嵌套的動態數組。 代碼: -在jQuery中獲取動態數組的值

new Ajax.Request(url, { // url of the controller 
method: 'get', 
parameters: { search: searchString }, //parameter 
onComplete: function(transport) { 
    alert(transport.responseText); 
     var values = transport.responseText; 
     alert(values.length); 
     // response from the controller will get alerted. 
    for(var i=0;i<values.length;i++){ 
for (var x in values[i]){ 
    console.log(x+"="+values[i][x]); 
    } 
} 


} 
}); 

當我警覺陣列則表明數組是這樣的: -

Array 
    (
    [0] => Array 
    (
     [post_id] => 1 
     [title] => Womens Health 
     [post_content] => Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
     [status] => 1 
     [created_time] => 2014-01-20 20:15:34 
     [update_time] => 2014-02-27 18:51:42 
     [identifier] => women_health 
     [user] => aheadWorks 
     [update_user] => Bod christen 
     [meta_keywords] => Must-have Magento Extensions, aheadWorks 
     [meta_description] => Must-have Magento Extensions 
     [comments] => 0 
     [tags] => 
     [short_content] => <p><img src="{{skin url='images/journal.jpeg'}}" alt="" /></p> 
    ) 

[1] => Array 
    (
     [post_id] => 2 
     [title] => Ladies Home Joural 
     [post_content] => <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry</p> 
     [status] => 1 
     [created_time] => 2014-02-17 18:02:54 
     [update_time] => 2014-02-27 18:49:46 
     [identifier] => ladies_home_joural 
     [user] => Bod christen 
     [update_user] => Bod christen 
     [meta_keywords] => 
     [meta_description] => 
     [comments] => 0 
     [tags] => 
     [short_content] => <p><img src="{{skin url='images/journal1.jpeg'}}" alt="" /></p> 
    ) 

) 
+0

提示....使用'for each'循環。看看這個:http://stackoverflow.com/questions/9329446/for-each-in-an-array-how-to-do-that-in-javascript –

+0

我想得到,但它返回undefined –

+0

發佈您正在嘗試使用的代碼 –

回答

0

試試這個:

假設你打電話給你數組values

for(var i=0;i<values.length;i++){ 
    for (var x in values[i]){ 
     console.log(x+"="+values[i][x]); 
    } 
} 
0

我不知道我理解你的問題,但這樣的事情可能工作:

for(var i=0; i < your_array.length; ++i){ 
    console.log(your_array[i]['title']); 
    console.log(your_array[i]['short_content']); 
} 
+0

我認爲問題在於他不知道密鑰是標題和short_content ...所以他無法對它們進行編碼 –

+0

對不起,但它不工作,發送未定義的數組 –

+0

無法正常工作, 請幫幫我 –