2016-08-01 40 views
0

這個JSON爲什麼不起作用?我試圖循環所有的畫廊陣列項目。我可能會將它們追加到表格中,但我甚至無法console.log這些值。我嘗試過使用鍵值,但沒有運氣!最重要的是,下面的圖庫JSON具有像204473,204474這些動態的值,所以我需要把它們當作我認爲的關鍵。如何加載一個動態的JSON數組?

function changeImages(data){ 

    // JSON 
    var data = data; 

    // Loop through all the JSON 
    for (var key in data) { 
     if (data.hasOwnProperty(key)) { 
      // Get the gallery array 
      var value = data[key]["gallery"]; 
      for (var keyI in value){ 
       // Get the keys 
       console.log(value[keyI].large); 
      } 
     } 
    } 
} 

function onPageLoad(){ 

    var strProductId = id; 

    // POST MAGENTO MAGE API CALL 
    jQuery.ajax({ 
     type: "POST", 
     url: "/swatches/ajax/media", 
     data: "product_id="+strProductId, 
     success: function(data){ 
      changeImages(data) 
     } 
    }); 

} 

onPageLoad(64413); 

所以我得到我的JSON通過$ .post它返回罰款。這是樣品JSON:

{ 
    "large": "https:\/\/res-1.xxxx.net\/image\/upload\/dpr_auto,f_auto,q_auto,w_auto\/d_coming-soon.jpg\/media\/catalog\/product\/8\/8\/886535528239_1.jpg", 
    "medium": "https:\/\/res-1.xxxx.net\/image\/upload\/dpr_auto,f_auto,q_auto,w_auto\/d_coming-soon.jpg\/media\/catalog\/product\/8\/8\/886535528239_1.jpg", 
    "small": "https:\/\/res-1.xxxx.net\/image\/upload\/dpr_auto,f_auto,q_auto,w_auto\/d_coming-soon.jpg\/media\/catalog\/product\/8\/8\/886535528239_1.jpg", 
    "gallery": { 
     "204472": { 
      "large": "https:\/\/res-1.xxxx.net\/image\/upload\/dpr_auto,f_auto,q_auto,w_auto\/d_coming-soon.jpg\/media\/catalog\/product\/8\/8\/886535528239_1.jpg", 
      "medium": "https:\/\/res-1.xxxx.net\/image\/upload\/dpr_auto,f_auto,q_auto,w_auto\/d_coming-soon.jpg\/media\/catalog\/product\/8\/8\/886535528239_1.jpg", 
      "small": "https:\/\/res-1.xxxx.net\/image\/upload\/dpr_auto,f_auto,q_auto,w_auto\/d_coming-soon.jpg\/media\/catalog\/product\/8\/8\/886535528239_1.jpg" 
     }, 
     "204473": { 
      "large": "https:\/\/res-4.xxxx.net\/image\/upload\/dpr_auto,f_auto,q_auto,w_auto\/d_coming-soon.jpg\/media\/catalog\/product\/8\/8\/886535528239_2.jpg", 
      "medium": "https:\/\/res-4.xxxx.net\/image\/upload\/dpr_auto,f_auto,q_auto,w_auto\/d_coming-soon.jpg\/media\/catalog\/product\/8\/8\/886535528239_2.jpg", 
      "small": "https:\/\/res-4.xxxx.net\/image\/upload\/dpr_auto,f_auto,q_auto,w_auto\/d_coming-soon.jpg\/media\/catalog\/product\/8\/8\/886535528239_2.jpg" 
     }, 
     "204474": { 
      "large": "https:\/\/res-5.xxxx.net\/image\/upload\/dpr_auto,f_auto,q_auto,w_auto\/d_coming-soon.jpg\/media\/catalog\/product\/8\/8\/886535528239_3.jpg", 
      "medium": "https:\/\/res-5.xxxx.net\/image\/upload\/dpr_auto,f_auto,q_auto,w_auto\/d_coming-soon.jpg\/media\/catalog\/product\/8\/8\/886535528239_3.jpg", 
      "small": "https:\/\/res-5.xxxx.net\/image\/upload\/dpr_auto,f_auto,q_auto,w_auto\/d_coming-soon.jpg\/media\/catalog\/product\/8\/8\/886535528239_3.jpg" 
     }, 
     "204475": { 
      "large": "https:\/\/res-3.xxxx.net\/image\/upload\/dpr_auto,f_auto,q_auto,w_auto\/d_coming-soon.jpg\/media\/catalog\/product\/8\/8\/886535528239_4.jpg", 
      "medium": "https:\/\/res-3.xxxx.net\/image\/upload\/dpr_auto,f_auto,q_auto,w_auto\/d_coming-soon.jpg\/media\/catalog\/product\/8\/8\/886535528239_4.jpg", 
      "small": "https:\/\/res-3.xxxx.net\/image\/upload\/dpr_auto,f_auto,q_auto,w_auto\/d_coming-soon.jpg\/media\/catalog\/product\/8\/8\/886535528239_4.jpg" 
     }, 
     "204476": { 
      "large": "https:\/\/res-5.xxxx.net\/image\/upload\/dpr_auto,f_auto,q_auto,w_auto\/d_coming-soon.jpg\/media\/catalog\/product\/8\/8\/886535528239_5.jpg", 
      "medium": "https:\/\/res-5.xxxx.net\/image\/upload\/dpr_auto,f_auto,q_auto,w_auto\/d_coming-soon.jpg\/media\/catalog\/product\/8\/8\/886535528239_5.jpg", 
      "small": "https:\/\/res-5.xxxx.net\/image\/upload\/dpr_auto,f_auto,q_auto,w_auto\/d_coming-soon.jpg\/media\/catalog\/product\/8\/8\/886535528239_5.jpg" 
     } 
    } 
} 
+0

有效的JSON以對象字面量開始。它不應該以數組的形式 –

+2

json可以從一個數組開始... http://stackoverflow.com/questions/5034444/can-a-json-start-with –

+0

@PierreInglebert感謝那+ 1還有回答的機會嗎? – TheBlackBenzKid

回答

1

你必須修改你的處理邏輯:

function changeImages(data){ 
    var gallery = data["gallery"]; 
    for (var key in gallery){ 
     // show all the gallery large images and loop through them 
     console.log(gallery[key].large); 
    } 
} 

我所做的只是去掉外for ... in和重命名幾個變量,使它更清潔。

根據你最初的問題,你期待你的數據是在一個數組,但它不是。

所以,如果你修改了第二條線,這將解決了太,雖然上面的回答是比較乾淨的,我也不會在數組做for ... in,尤其是其中只有一個元素:

function changeImages(data){ 

    // JSON 
    var data = [data]; //Note: I changed this line 

    // Loop through all the JSON 
    for (var key in data) { 
     if (data.hasOwnProperty(key)) { 
      // Get the gallery array 
      var value = data[key]["gallery"]; 
      for (var keyI in value){ 
       // Get the keys 
       console.log(value[keyI].large); 
      } 
     } 
    } 
}