使用jQuery访问多维JSON数组中的数据

问题描述:

我正在努力解决如何访问基本上多维的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

在您的成功函数中,您可以将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

每个产品的详细信息都可以通过data[iProductIndex.toString()]成员进行访问。数据存储在data["0"]data["1"]之内,因此要访问它们,您需要将整数值转换为字符串。不幸的是,您将无法使用$.each循环,因为“0”和“1”是单独的成员对象。与iProductIndex一起使用循环。

+0

最好的解决办法一个小例子的任何机会呢?我对jquery比较陌生。 – dangermark 2011-04-11 06:34:31

数据,萨尔曼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”]);

试试这个

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