使用 jQuery 访问多维 JSON 数组中的数据
我正在尝试弄清楚如何访问本质上多维 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 访问第一个维度中的所有内容,但我不确定如何为下一个维度建立索引。显然我想以某种方式展示每一种产品。
非常欢迎提出建议。
I am trying to work out how to access data in an essentially multidimensional JSON array.
My jQuery AJAX request looks like this:
$("#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'
});
});
This is what i am getting back. User account details, plus a list of products they have against their account.
{
"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"
}
}
As you can see, i am alerting the first name, which is fine. I can access everything in the first dimension by using data.key but i'm not sure how then i need to index the next dimension. Obviously I would like to display each of the products somehow.
Suggestions would be most welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 success 函数中,您可以将 JSON 数据视为 JavaScript 对象。您可以像这样访问产品数组及其内部的对象:
如果您不知道控制台是什么,请将
console.log
替换为alert
。Inside your success function you can treat the JSON data as a JavaScript object. You can access the product array and objects inside it like this:
Replace
console.log
withalert
if you do not know what console is.每个产品详细信息都可以通过
data[iProductIndex.toString()]
成员访问。数据存储在data["0"]
和data["1"]
内部,因此要访问它们,您需要将整数值转换为字符串。不幸的是,您将无法使用$.each
循环,因为“0”和“1”是单独的成员对象。将 for 循环与iProductIndex
结合使用。Each of the product details can be accessed through
data[iProductIndex.toString()]
member. Data is stored insidedata["0"]
anddata["1"]
, therefore to access them you need to convert integer value to string. Unfortunately you won't be able to use$.each
loop because "0" and "1" are separate member objects. Use for loop withiProductIndex
.Salman A,提供的数据不允许您回答。请参阅 JSON 数组 了解数组定义,使其正常工作按照你的方式,它必须被定义为
To OP:
警报(数据[“0”]。产品编号);
警报(数据[“1”][“购买日期”]);
Data supplied does not allow for your answer, Salman A. See JSON Arrays for array definition, to have it work your way it must've been defined as
To OP:
alert(data["0"].product_no);
alert(data["1"]["date_of_purchase"]);
试试这个
try this