jquery
  • json
  • multidimensional-array
  • 2011-04-11 99 views 4 likes 
    4

    我正在努力解決如何訪問基本上多維的JSON數組中的數據。使用jQuery訪問多維JSON數組中的數據

    我的jQuery AJAX請求是這樣的:

    $("#login-form").submit(function(e) { 
    e.preventDefault(); 
    $.ajax({ 
        type: 'POST', 
        url: '/ajax/login', 
        data: 'email='+$("#email").val()+'&password='+$("#password").val(), 
        success: function(data){ 
    
        // FIRE ALERT HERE   
        alert(data.firstname); 
    
        }, 
        dataType: 'json' 
        }); 
    }); 
    

    這就是我收到了。用戶帳戶詳細信息以及他們針對其帳戶的產品列表。

    { 
        "logged_in":true, 
        "firstname":"Joe", 
        "surname":"Bloggs", 
        "Full_name":"Joe Bloggs", 
        "email":"[email protected]", 
        "phone":"+123456789", 
        "website":"", 
        "age":"26-35", 
        "street":"1 Street Ave", 
        "city":"Townland", 
        "state":"NA", 
        "postcode":"1234", 
        "country":"Australia", 
        "products":2, 
        "0":{ 
         "product_no":"1087", 
         "customer":"2", 
         "bought_from":"1", 
         "date_of_purchase":"2011-04-08", 
         "method":"instore", 
         "invoice":"0", 
         "current":"1" 
        }, 
        "1":{ 
         "product_no":"24", 
         "customer":"2", 
         "bought_from":"1", 
         "date_of_purchase":"2011-04-08", 
         "method":"instore", 
         "invoice":"0", 
         "current":"1" 
        } 
    } 
    

    正如你所看到的,我在提醒名字,這很好。我可以通過使用data.key訪問第一維中的所有內容,但是我不確定那麼我需要索引下一個維度。很顯然,我想以某種方式顯示每個產品。

    建議將是最受歡迎的。

    +0

    如果u [R使用* PHP *然後'json_encode'是爲您的數據 – diEcho 2011-04-11 07:01:48

    回答

    1

    在您的成功函數中,您可以將JSON數據視爲JavaScript對象。您可以像這樣訪問產品陣列和其中的對象:

    console.log(data.products.length +「data in data」);對於(var i = 0; i < data.products.length; i ++){ var product = data.products [i]; console.log(product.product_no); //同樣,對於其他領域 }

    console.log(data.products + " product(s) in data"); // data.products is 2 (integer) 
    for(var i = 0; i < data.products; i++) {   // 
        var product = data[i.toString()];    // 0.toString() is "0" 
                    // data["0"] is what you want 
                    // now product points to the property "0" 
        console.log(product.product_no);    // so you can use product.xxx 
                    // or product["xxx"] 
    }             // likewise for "1", "2", "3" and so on 
    

    更換console.logalert如果你不知道什麼是控制檯。

    提供不允許你的答案
    +0

    'data.products.length'返回'undefined' bcoz「0」和「1」不是'products'的子元素 – diEcho 2011-04-11 06:58:38

    +0

    @diEcho:謝謝指出。我編輯了我的答案。 – 2011-04-11 07:54:00

    +0

    這正是我所期待的。謝謝你讓我朝正確的方向傾斜 – dangermark 2011-04-11 21:41:24

    1

    每個產品的詳細信息都可以通過data[iProductIndex.toString()]成員進行訪問。數據存儲在data["0"]data["1"]之內,因此要訪問它們,您需要將整數值轉換爲字符串。不幸的是,您將無法使用$.each循環,因爲「0」和「1」是單獨的成員對象。與iProductIndex一起使用循環。

    +0

    最好的解決辦法一個小例子的任何機會呢?我對jquery比較陌生。 – dangermark 2011-04-11 06:34:31

    0

    數據,薩爾曼A.數組定義見JSON Arrays,有它自己的方式它一定是被定義爲

    {"products" : [ {"product_no":"1087", 
         "customer":"2", 
         "bought_from":"1", 
         "date_of_purchase":"2011-04-08", 
         "method":"instore", 
         "invoice":"0", 
         "current":"1"} ] } 
    

    到OP: 警報(數據[ 「0」] product_no); alert(data [「1」] [「date_of_purchase」]);

    0

    試試這個

    <script type="text/javascript"> 
    var json_string={ 
        "logged_in":true, 
        "firstname":"Joe", 
        "surname":"Bloggs", 
        "Full_name":"Joe Bloggs", 
        "email":"[email protected]", 
        "phone":"+123456789", 
        "website":"", 
        "age":"26-35", 
        "street":"1 Street Ave", 
        "city":"Townland", 
        "state":"NA", 
        "postcode":"1234", 
        "country":"Australia", 
        "products":2, 
        "0":{ 
         "product_no":"1087", 
         "customer":"2", 
         "bought_from":"1", 
         "date_of_purchase":"2011-04-08", 
         "method":"instore", 
         "invoice":"0", 
         "current":"1" 
        }, 
        "1":{ 
         "product_no":"24", 
         "customer":"2", 
         "bought_from":"1", 
         "date_of_purchase":"2011-04-08", 
         "method":"instore", 
         "invoice":"0", 
         "current":"1" 
        } 
    }; 
    
    for (key in json_string) { 
    // Most modern browsers should have hasOwnProperty by now. 
    // This keeps us from getting farther up the chain. 
    if (json_string.hasOwnProperty(key)) { 
    document.write(key + "->" + json_string[key]); 
    document.write("<br>"); 
    } 
    }; 
    
    
    var pro_1= json_string[0]; // here u change 0 with 1 and get the data of "1" 
    
    for (key in pro_1) { 
    if (pro_1.hasOwnProperty(key)) { 
    document.write(key + "->" + pro_1[key]); 
    document.write("<br>"); 
    } 
    }; 
    
    </script> 
    
    相關問題