Javascript 使用jQuery访问多维JSON数组中的数据
我正在尝试解决如何在本质上是多维的JSON数组中访问数据 我的jQuery AJAX请求如下所示:Javascript 使用jQuery访问多维JSON数组中的数据,javascript,jquery,arrays,json,ajax,Javascript,Jquery,Arrays,Json,Ajax,我正在尝试解决如何在本质上是多维的JSON数组中访问数据 我的jQuery AJAX请求如下所示: $("#login-form").submit(function(e) { e.preventDefault(); $.ajax({ type: 'POST', url: '/ajax/login', data: 'email='+$("#email").val()+'&password='+$("#password").val(), success: fun
$("#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'
});
});
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
这就是我得到的。用户帐户详细信息,以及他们针对其帐户拥有的产品列表
{
"logged_in":true,
"firstname":"Joe",
"surname":"Bloggs",
"Full_name":"Joe Bloggs",
"email":"email@website.com",
"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访问第一个维度中的所有内容,但我不确定接下来需要如何索引下一个维度。显然,我想以某种方式展示每种产品
非常欢迎提供建议。每个产品详细信息都可以通过
数据[IPProductIndex.toString()]
成员访问。数据存储在数据[“0”]
和数据[“1”]
中,因此要访问它们,需要将整数值转换为字符串。很遗憾,您将无法使用$。每个循环都是因为“0”和“1”是独立的成员对象。在success函数中使用for循环和ipProductIndex
可以将JSON数据视为JavaScript对象。您可以访问产品阵列及其内部的对象,如下所示:
$("#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'
});
});
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.log(data.products+“数据中的产品”);//data.products为2(整数)
对于(var i=0;i
如果您不知道控制台是什么,请将console.log
替换为alert
。提供的数据不允许您回答,Salman A。有关数组定义,请参阅,要使其按您的方式工作,必须将其定义为
{"products" : [ {"product_no":"1087",
"customer":"2",
"bought_from":"1",
"date_of_purchase":"2011-04-08",
"method":"instore",
"invoice":"0",
"current":"1"} ] }
要执行以下操作:
警报(数据[“0”]。产品编号);
警报(数据[“1”][“购买日期]) 试试这个
<script type="text/javascript">
var json_string={
"logged_in":true,
"firstname":"Joe",
"surname":"Bloggs",
"Full_name":"Joe Bloggs",
"email":"email@website.com",
"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>
var-json_字符串={
“登录”:正确,
“名字”:“乔”,
“姓氏”:“布洛格斯”,
“全名”:“乔·布洛格斯”,
“电子邮件”:email@website.com",
“电话”:“+123456789”,
“网站”:“,
“年龄”:“26-35岁”,
“街道”:“1街大街”,
“城市”:“城镇”,
“国家”:“不适用”,
“邮政编码”:“1234”,
“国家”:“澳大利亚”,
"产品":二,,
"0":{
“产品编号”:“1087”,
“客户”:“2”,
“购买自”:“1”,
“购买日期”:“2011-04-08”,
“方法”:“instore”,
“发票”:“0”,
“当前”:“1”
},
"1":{
“产品编号”:“24”,
“客户”:“2”,
“购买自”:“1”,
“购买日期”:“2011-04-08”,
“方法”:“instore”,
“发票”:“0”,
“当前”:“1”
}
};
for(输入json_字符串){
//现在大多数现代浏览器都应该有自己的属性了。
//这使我们无法在链条上走得更远。
if(json_string.hasOwnProperty(key)){
document.write(key+“->”+json_字符串[key]);
文件。写(“
”);
}
};
var pro_1=json_字符串[0];//这里你用1改变0,得到“1”的数据
用于(输入pro_1){
if(pro_1.hasOwnProperty(键)){
编写(键+“->”+pro_1[key]);
文件。写(“
”);
}
};
有没有可能举个小例子?我对jquery比较陌生。data.products.length
返回undefined
bcoz,即“0”和“1”不是products的子项
这正是我想要的。感谢您告诉我正确的方向如果您使用php,那么json\u encode
是您数据的最佳解决方案