如何加载动态的JSON数组?

时间:2016-08-01 08:19:00

标签: javascript json key

为什么这个JSON不起作用?我正在尝试循环所有图库数组大型项目。我将来可能会将它们附加到表或其他内容中,但我甚至无法控制它。记录值。我尝试过使用键值,但没有运气!最重要的是,下面的画廊JSON的价值如204473,204474这些是动态的,所以我需要把它们看作是我认为的关键吗?

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);

所以我通过$ .post得到我的JSON,返回正常。这是示例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"
        }
    }
}

1 个答案:

答案 0 :(得分: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);
            }
        }
    }
}