这是我的json代码,它返回一个数组" Customers"包含objets中的对象和数组
这是我的json cod:
{
"Customers": [
{
"customerData": {
"secondLastName": "Apale",
"firstLastName": "Lara",
"phoneNumber": "2711292033",
"address": "Calle X avenida Y #100",
"paymentCapacity": 18000,
"gender": "Femenino",
"name": "Yessica",
},
"orders": [
{
"amount": 34371,
"term": "2017-07-21T17:32:28Z",
"payment": 1423,
"id": 12345678,
"calculationDate": "2017-07-21T17:32:28Z",
"products": [
{
"SKUNumber": 28005417,
"quantity": 1,
"SKULineDescription": "Computadoras",
"SKUDescription": "Laptop HP",
"SKULineId": 4
}
]
}
]
},
{
"customerData": {
"secondLastName": "González",
"firstLastName": "Pineda",
"phoneNumber": "55678420",
"address": "Calle 26 #4732 Col. Pradera",
"paymentCapacity": 180,
"gender": "Femenino",
"name": "María",
},
"orders": [
{
"amount": 34371,
"term": "2017-07-21T17:32:28Z",
"payment": 1423,
"id": 12678422,
"calculationDate": "2017-07-21T17:32:28Z",
"products": [
{
"SKUNumber": 28005417,
"quantity": 1,
"SKULineDescription": "Computadoras",
"SKUDescription": "Laptop HP",
"SKULineId": 4
}
]
}
]
}
]
}
这是声明我的数组:arrCustomers = new Array(); 我尝试用foreach遍历json,我有错误说:undefined,这是我的控制台
答案 0 :(得分:0)
我猜你在访问数组和对象的元素时遇到了麻烦。如果是这种情况,那么这可以帮助你。
另外,运行那些被评论的片段。
您可以通过两种方式访问对象属性,一种是使用点(。)运算符,另一种是在示例中使用。
var array = {
"Customers": [
{
"customerData": {
"secondLastName": "Apale",
"firstLastName": "Lara",
"phoneNumber": "2711292033",
"address": "Calle X avenida Y #100",
"paymentCapacity": 18000,
"gender": "Femenino",
"name": "Yessica",
},
"orders": [
{
"amount": 34371,
"term": "2017-07-21T17:32:28Z",
"payment": 1423,
"id": 12345678,
"calculationDate": "2017-07-21T17:32:28Z",
"products": [
{
"SKUNumber": 28005417,
"quantity": 1,
"SKULineDescription": "Computadoras",
"SKUDescription": "Laptop HP",
"SKULineId": 4
}
]
}
]
},
{
"customerData": {
"secondLastName": "González",
"firstLastName": "Pineda",
"phoneNumber": "55678420",
"address": "Calle 26 #4732 Col. Pradera",
"paymentCapacity": 180,
"gender": "Femenino",
"name": "María",
},
"orders": [
{
"amount": 34371,
"term": "2017-07-21T17:32:28Z",
"payment": 1423,
"id": 12678422,
"calculationDate": "2017-07-21T17:32:28Z",
"products": [
{
"SKUNumber": 28005417,
"quantity": 1,
"SKULineDescription": "Computadoras",
"SKUDescription": "Laptop HP",
"SKULineId": 4
}
]
}
]
}
]
}
console.log("the whole array ==>",array);
//console.log("customers attribute ==>",array["Customers"]);
//console.log("first customerData ==>",array["Customers"][0]);
//console.log("first customerData's order ==>",array["Customers"][0]["orders"]);
//console.log("first customerData's all products ==>",array["Customers"][0]["orders"].map(i => i["products"]));
//console.log("All customerDatas", array["Customers"].map(i => i["customerData"]));
//console.log("All orders", array["Customers"].map(i => i["orders"]));
//console.log("All orders of all customers", array["Customers"].map(i => i["orders"]));